From b2996f10ff841ad1583d4c1d73e9b82f91897aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 26 Apr 2018 12:56:07 +0200 Subject: [PATCH] Get rid of a bit of Python2 only code --- src/octoprint/server/__init__.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index e618df0f3..66d539c04 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -1478,20 +1478,25 @@ class Server(object): loginManager.init_app(app, add_context_processor=False) def _start_intermediary_server(self): - import BaseHTTPServer - import SimpleHTTPServer + try: + # noinspection PyCompatibility + from http.server import HTTPServer, BaseHTTPRequestHandler + except ImportError: + # noinspection PyCompatibility + from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler + import threading import socket host = self._host port = self._port - class IntermediaryServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): + class IntermediaryServerHandler(BaseHTTPRequestHandler): def __init__(self, rules=None, *args, **kwargs): if rules is None: rules = [] self.rules = rules - SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, *args, **kwargs) + BaseHTTPRequestHandler.__init__(self, *args, **kwargs) def do_GET(self): request_path = self.path @@ -1541,11 +1546,11 @@ class Server(object): rules = map(process, filter(lambda rule: len(rule) == 2 or len(rule) == 3, rules)) - class HTTPServerV6(BaseHTTPServer.HTTPServer): + class HTTPServerV6(HTTPServer): address_family = socket.AF_INET6 def __init__(self, *args, **kwargs): - BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs) + HTTPServer.__init__(self, *args, **kwargs) # make sure to enable dual stack mode, otherwise the socket might only listen on IPv6 self.socket.setsockopt(octoprint.util.net.IPPROTO_IPV6, octoprint.util.net.IPV6_V6ONLY, 0) @@ -1555,7 +1560,7 @@ class Server(object): ServerClass = HTTPServerV6 else: # v4 - ServerClass = BaseHTTPServer.HTTPServer + ServerClass = HTTPServer self._logger.debug("Starting intermediary server on http://{}:{}".format(host if not ":" in host else "[" + host + "]", port))