[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
master
frozar 6 years ago committed by David O'Connor
parent 61f5fa481d
commit 0d028f4a9d

@ -4,6 +4,7 @@ import http.server
import os import os
import socketserver import socketserver
import urllib import urllib
import sys
PORT = 8000 PORT = 8000
@ -26,7 +27,15 @@ handler.extensions_map.update({
}) })
socketserver.TCPServer.allow_reuse_address = True 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 httpd.allow_reuse_address = True
print("Serving at port", PORT) print("Serving at port", PORT)
httpd.serve_forever() httpd.serve_forever()
httpd.serve_close()

Loading…
Cancel
Save