Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3)

Side by Side Diff: Lib/http/server.py

Issue 26404: socketserver context manager
Patch Set: Created 4 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """HTTP server classes. 1 """HTTP server classes.
2 2
3 Note: BaseHTTPRequestHandler doesn't implement any HTTP request; see 3 Note: BaseHTTPRequestHandler doesn't implement any HTTP request; see
4 SimpleHTTPRequestHandler for simple implementations of GET, HEAD and POST, 4 SimpleHTTPRequestHandler for simple implementations of GET, HEAD and POST,
5 and CGIHTTPRequestHandler for CGI scripts. 5 and CGIHTTPRequestHandler for CGI scripts.
6 6
7 It does, however, optionally implement HTTP/1.1 persistent connections, 7 It does, however, optionally implement HTTP/1.1 persistent connections,
8 as of version 0.3. 8 as of version 0.3.
9 9
10 Notes on CGIHTTPRequestHandler 10 Notes on CGIHTTPRequestHandler
(...skipping 1156 matching lines...) Expand 10 before | Expand all | Expand 10 after
1167 def test(HandlerClass=BaseHTTPRequestHandler, 1167 def test(HandlerClass=BaseHTTPRequestHandler,
1168 ServerClass=HTTPServer, protocol="HTTP/1.0", port=8000, bind=""): 1168 ServerClass=HTTPServer, protocol="HTTP/1.0", port=8000, bind=""):
1169 """Test the HTTP request handler class. 1169 """Test the HTTP request handler class.
1170 1170
1171 This runs an HTTP server on port 8000 (or the port argument). 1171 This runs an HTTP server on port 8000 (or the port argument).
1172 1172
1173 """ 1173 """
1174 server_address = (bind, port) 1174 server_address = (bind, port)
1175 1175
1176 HandlerClass.protocol_version = protocol 1176 HandlerClass.protocol_version = protocol
1177 httpd = ServerClass(server_address, HandlerClass) 1177 with ServerClass(server_address, HandlerClass) as httpd:
1178 1178
Martin Panter 2016/02/24 02:55:12 Also I think this blank line could be dropped
palaviv 2016/02/24 10:05:34 Done.
1179 sa = httpd.socket.getsockname() 1179 sa = httpd.socket.getsockname()
1180 print("Serving HTTP on", sa[0], "port", sa[1], "...") 1180 print("Serving HTTP on", sa[0], "port", sa[1], "...")
1181 try: 1181 try:
1182 httpd.serve_forever() 1182 httpd.serve_forever()
1183 except KeyboardInterrupt: 1183 except KeyboardInterrupt:
1184 print("\nKeyboard interrupt received, exiting.") 1184 print("\nKeyboard interrupt received, exiting.")
1185 httpd.server_close() 1185 sys.exit(0)
1186 sys.exit(0)
1187 1186
1188 if __name__ == '__main__': 1187 if __name__ == '__main__':
1189 parser = argparse.ArgumentParser() 1188 parser = argparse.ArgumentParser()
1190 parser.add_argument('--cgi', action='store_true', 1189 parser.add_argument('--cgi', action='store_true',
1191 help='Run as CGI Server') 1190 help='Run as CGI Server')
1192 parser.add_argument('--bind', '-b', default='', metavar='ADDRESS', 1191 parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
1193 help='Specify alternate bind address ' 1192 help='Specify alternate bind address '
1194 '[default: all interfaces]') 1193 '[default: all interfaces]')
1195 parser.add_argument('port', action='store', 1194 parser.add_argument('port', action='store',
1196 default=8000, type=int, 1195 default=8000, type=int,
1197 nargs='?', 1196 nargs='?',
1198 help='Specify alternate port [default: 8000]') 1197 help='Specify alternate port [default: 8000]')
1199 args = parser.parse_args() 1198 args = parser.parse_args()
1200 if args.cgi: 1199 if args.cgi:
1201 handler_class = CGIHTTPRequestHandler 1200 handler_class = CGIHTTPRequestHandler
1202 else: 1201 else:
1203 handler_class = SimpleHTTPRequestHandler 1202 handler_class = SimpleHTTPRequestHandler
1204 test(HandlerClass=handler_class, port=args.port, bind=args.bind) 1203 test(HandlerClass=handler_class, port=args.port, bind=args.bind)
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+