From 69cf59b633864a1dfb15c8e778ca538db9485228 Mon Sep 17 00:00:00 2001 From: Tatu Aalto Date: Fri, 17 Mar 2017 14:04:26 +0200 Subject: [PATCH] Fixes problem with test server on windows Also supports Python 3 now. --- test/resources/testserver/testserver.py | 107 +++++++----------------- 1 file changed, 29 insertions(+), 78 deletions(-) diff --git a/test/resources/testserver/testserver.py b/test/resources/testserver/testserver.py index 8a0058db0..19e82efc9 100644 --- a/test/resources/testserver/testserver.py +++ b/test/resources/testserver/testserver.py @@ -1,106 +1,57 @@ +# Initially based on: # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/336012 -import os -import httplib -import SimpleHTTPServer -import BaseHTTPServer - +from __future__ import print_function -class StoppableHttpRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): +import os +import sys +try: + from httplib import HTTPConnection + from BaseHTTPServer import HTTPServer + from SimpleHTTPServer import SimpleHTTPRequestHandler + from SocketServer import ThreadingMixIn +except ImportError: # Python 3 + from http.client import HTTPConnection + from http.server import SimpleHTTPRequestHandler, HTTPServer + from socketserver import ThreadingMixIn + + +class StoppableHttpRequestHandler(SimpleHTTPRequestHandler): """http request handler with QUIT stopping the server""" def do_QUIT(self): - """send 200 OK response, and set server.stop to True""" self.send_response(200) self.end_headers() - self.server.stop = True - + self.server.shutdown() + self.server.server_close() + def do_POST(self): - # We could also process paremeters here using something like below. - # length = self.headers['Content-Length'] - # print self.rfile.read(int(length)) self.do_GET() - def send_head(self): - # This is ripped directly from SimpleHTTPRequestHandler, - # only the cookie part is added. - """Common code for GET and HEAD commands. - This sends the response code and MIME headers. - - Return value is either a file object (which has to be copied - to the outputfile by the caller unless the command was HEAD, - and must be closed by the caller under all circumstances), or - None, in which case the caller has nothing further to do. - - """ - path = self.translate_path(self.path) - f = None - if os.path.isdir(path): - if not self.path.endswith('/'): - # redirect browser - doing basically what apache does - self.send_response(301) - self.send_header("Location", self.path + "/") - self.end_headers() - return None - for index in "index.html", "index.htm": - index = os.path.join(path, index) - if os.path.exists(index): - path = index - break - else: - return self.list_directory(path) - ctype = self.guess_type(path) - if ctype.startswith('text/'): - mode = 'r' - else: - mode = 'rb' - try: - f = open(path, mode) - except IOError: - self.send_error(404, "File not found") - return None - self.send_response(200) - self.send_header("Content-type", ctype) - fs = os.fstat(f.fileno()) - self.send_header("Content-Length", str(fs[6])) - self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) - self.end_headers() - return f - - -class StoppableHttpServer(BaseHTTPServer.HTTPServer): - """http server that reacts to self.stop flag""" - - def serve_forever(self): - """Handle one request at a time until stopped.""" - self.stop = False - while not self.stop: - self.handle_request() +class ThreadingHttpServer(ThreadingMixIn, HTTPServer): + pass def stop_server(port=7000): """send QUIT request to http server running on localhost:""" - conn = httplib.HTTPConnection("localhost:%d" % port) + conn = HTTPConnection("localhost:%d" % port) conn.request("QUIT", "/") conn.getresponse() -def start_server(port=7000): - import os - os.chdir(os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), '..')) - server = StoppableHttpServer(('', port), StoppableHttpRequestHandler) +def start_server(path, port=7000): + os.chdir(path) + server = ThreadingHttpServer(('', port), StoppableHttpRequestHandler) server.serve_forever() - - + + if __name__ == '__main__': - import sys if len(sys.argv) != 2 or sys.argv[1] not in ['start', 'stop']: print('usage: %s start|stop' % sys.argv[0]) sys.exit(1) if sys.argv[1] == 'start': - start_server() + path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + start_server(path) else: stop_server() - -