diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-04-13 00:36:52 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-04-13 00:36:52 (GMT) |
commit | 0cab9c1ebaa11bb7838a552c671c903156262ab7 (patch) | |
tree | 640bcabc8876c74c7ba53231551613cfb9a34600 /Lib/http | |
parent | 7258176c68a5061a5d05ee43f11e99fd94e34364 (diff) | |
download | cpython-0cab9c1ebaa11bb7838a552c671c903156262ab7.zip cpython-0cab9c1ebaa11bb7838a552c671c903156262ab7.tar.gz cpython-0cab9c1ebaa11bb7838a552c671c903156262ab7.tar.bz2 |
Issue #26404: Add context manager to socketserver, by Aviv Palivoda
Diffstat (limited to 'Lib/http')
-rw-r--r-- | Lib/http/server.py | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py index fbee6a9..c1607b3 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -1175,16 +1175,14 @@ def test(HandlerClass=BaseHTTPRequestHandler, server_address = (bind, port) HandlerClass.protocol_version = protocol - httpd = ServerClass(server_address, HandlerClass) - - sa = httpd.socket.getsockname() - print("Serving HTTP on", sa[0], "port", sa[1], "...") - try: - httpd.serve_forever() - except KeyboardInterrupt: - print("\nKeyboard interrupt received, exiting.") - httpd.server_close() - sys.exit(0) + with ServerClass(server_address, HandlerClass) as httpd: + sa = httpd.socket.getsockname() + print("Serving HTTP on", sa[0], "port", sa[1], "...") + try: + httpd.serve_forever() + except KeyboardInterrupt: + print("\nKeyboard interrupt received, exiting.") + sys.exit(0) if __name__ == '__main__': parser = argparse.ArgumentParser() |