From 0d028f4a9d094c46f493f35145358b5fef143783 Mon Sep 17 00:00:00 2001 From: frozar Date: Thu, 21 Mar 2019 11:39:00 +0100 Subject: [PATCH] [SCRIPT] Check the python interpretor version in serve.py If the python interpretor is to old, it doesn't have yet the context manager protocol support: https://stackoverflow.com/questions/48055880/attributeerror-exit-with-socketserver-on-python-3-4-3#answer-48056064 An alternative syntax is proposed to support python interpretor older than 3.6 --- serve.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/serve.py b/serve.py index 163f921..adffe91 100644 --- a/serve.py +++ b/serve.py @@ -4,6 +4,7 @@ import http.server import os import socketserver import urllib +import sys PORT = 8000 @@ -26,7 +27,15 @@ handler.extensions_map.update({ }) socketserver.TCPServer.allow_reuse_address = True -with socketserver.TCPServer(("", PORT), handler) as httpd: +# The context manager protocol is support only since python 3.6 and higher. +if (3 <= sys.version_info[0]) and (6 <= sys.version_info[1]): + with socketserver.TCPServer(("", PORT), handler) as httpd: + httpd.allow_reuse_address = True + print("Serving at port", PORT) + httpd.serve_forever() +else: + httpd = socketserver.TCPServer(("", PORT), handler) httpd.allow_reuse_address = True print("Serving at port", PORT) httpd.serve_forever() + httpd.serve_close()