forked from owtf/owtf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
96 lines (75 loc) · 2.4 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
tests.server
~~~~~~~~~~~~
Dummy server for the test runner.
"""
import os
import sys
import time
import signal
try:
import http.server as httplib
except:
import httplib
from multiprocessing import Process
import tornado.ioloop
import tornado.web
class DummyGetHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world!")
class DummyWebApplication(tornado.web.Application):
def log_request(self, handler):
pass
class WebServerProcess(object):
"""Handles a child process that executes the tornado application."""
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.process = None
def start(self):
"""
Creates a web server in another process and wait until it is ready to
handle requests.
"""
# Create the web application
self.application = DummyWebApplication([(r"/", DummyGetHandler)])
self.process = Process(
target=start_application, args=(self.application, self.ip, self.port)
)
self.process.start()
self.wait_until_server_is_ready()
def wait_until_server_is_ready(self):
while not self.server_is_ready():
# If it's not ready, wait for it
time.sleep(0.1)
def test_connection(self):
conn = httplib.HTTPConnection("%s:%s" % (self.ip, self.port))
conn.request("GET", "/")
def stop(self):
"""Stops the web server and wait until it has sucesfully cleaned up."""
if self.is_alive():
os.kill(self.process.pid, signal.SIGINT)
self.wait_for_server_shutdown()
self.process = None
def wait_for_server_shutdown(self):
while self.server_is_ready(): # Wait until server is down
time.sleep(0.1)
def server_is_ready(self):
try:
self.test_connection()
return True
except:
return False
def is_alive(self):
"""Test if the server process is alive and running."""
if self.process is None:
return False
else:
return self.process.is_alive()
def start_application(application, ip, port):
"""Callable to be executed in the subprocess."""
application.listen(str(port), address=ip)
try:
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt: # Exit cleanly
sys.exit(0)