Renamed dummyserver to testserver

This commit is contained in:
Braulio Valdivielso Martínez
2015-11-25 14:05:21 +01:00
parent b806ce15ba
commit 47e96c4e57
4 changed files with 3 additions and 6 deletions
Binary file not shown.
View File
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/python
import threading, socket
class Server(threading.Thread):
""" Basic socket server used for unit testing """
def __init__(self, handler, host='localhost', port=8021):
threading.Thread.__init__(self)
self.handler = handler
self.host = host
self.port = port
self.ready_event = threading.Event()
self.stop_event = threading.Event()
def run(self):
sock = socket.socket()
sock.bind((self.host, self.port))
sock.listen(0)
self.ready_event.set()
self.handler(sock)
self.stop_event.set()
sock.close()
def __enter__(self):
self.start()
self.ready_event.wait()
return self.host, self.port
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None:
self.stop_event.wait()
return False # allow exceptions to propagate