Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 9 additions & 59 deletions test/resources/testserver/testserver.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,27 @@
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/336012

import http.client
import http.server
import os
import httplib
import SimpleHTTPServer
import BaseHTTPServer


class StoppableHttpRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
class StoppableHttpRequestHandler(http.server.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

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))
# 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):
class StoppableHttpServer(http.server.HTTPServer):
"""http server that reacts to self.stop flag"""

def serve_forever(self):
Expand All @@ -81,7 +33,7 @@ def serve_forever(self):

def stop_server(port=7000):
"""send QUIT request to http server running on localhost:<port>"""
conn = httplib.HTTPConnection("localhost:%d" % port)
conn = http.client.HTTPConnection("localhost:%d" % port)
conn.request("QUIT", "/")
conn.getresponse()

Expand All @@ -91,8 +43,8 @@ def start_server(port=7000):
os.chdir(os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), '..'))
server = StoppableHttpServer(('', port), StoppableHttpRequestHandler)
server.serve_forever()


if __name__ == '__main__':
import sys
if len(sys.argv) != 2 or sys.argv[1] not in ['start', 'stop']:
Expand All @@ -102,5 +54,3 @@ def start_server(port=7000):
start_server()
else:
stop_server()