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

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

Issue 26404: socketserver context manager
Patch Set: Created 4 years, 1 month 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 sa = httpd.socket.getsockname()
1179 sa = httpd.socket.getsockname() 1179 print("Serving HTTP on", sa[0], "port", sa[1], "...")
1180 print("Serving HTTP on", sa[0], "port", sa[1], "...") 1180 try:
1181 try: 1181 httpd.serve_forever()
1182 httpd.serve_forever() 1182 except KeyboardInterrupt:
1183 except KeyboardInterrupt: 1183 print("\nKeyboard interrupt received, exiting.")
1184 print("\nKeyboard interrupt received, exiting.") 1184 sys.exit(0)
1185 httpd.server_close()
1186 sys.exit(0)
1187 1185
1188 if __name__ == '__main__': 1186 if __name__ == '__main__':
1189 parser = argparse.ArgumentParser() 1187 parser = argparse.ArgumentParser()
1190 parser.add_argument('--cgi', action='store_true', 1188 parser.add_argument('--cgi', action='store_true',
1191 help='Run as CGI Server') 1189 help='Run as CGI Server')
1192 parser.add_argument('--bind', '-b', default='', metavar='ADDRESS', 1190 parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
1193 help='Specify alternate bind address ' 1191 help='Specify alternate bind address '
1194 '[default: all interfaces]') 1192 '[default: all interfaces]')
1195 parser.add_argument('port', action='store', 1193 parser.add_argument('port', action='store',
1196 default=8000, type=int, 1194 default=8000, type=int,
1197 nargs='?', 1195 nargs='?',
1198 help='Specify alternate port [default: 8000]') 1196 help='Specify alternate port [default: 8000]')
1199 args = parser.parse_args() 1197 args = parser.parse_args()
1200 if args.cgi: 1198 if args.cgi:
1201 handler_class = CGIHTTPRequestHandler 1199 handler_class = CGIHTTPRequestHandler
1202 else: 1200 else:
1203 handler_class = SimpleHTTPRequestHandler 1201 handler_class = SimpleHTTPRequestHandler
1204 test(HandlerClass=handler_class, port=args.port, bind=args.bind) 1202 test(HandlerClass=handler_class, port=args.port, bind=args.bind)
OLDNEW
« Doc/whatsnew/3.6.rst ('K') | « Doc/whatsnew/3.6.rst ('k') | Lib/socketserver.py » ('j') | no next file with comments »

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