Made TestServer.Server listen on an arbitrary open port by default

This commit is contained in:
Braulio Valdivielso Martínez
2015-11-25 14:15:51 +01:00
parent 47e96c4e57
commit ab0f063d85
2 changed files with 5 additions and 2 deletions
Binary file not shown.
+5 -2
View File
@@ -3,9 +3,9 @@
import threading, socket
class Server(threading.Thread):
""" Basic socket server used for unit testing """
""" Dummy server using for unit testing """
def __init__(self, handler, host='localhost', port=8021):
def __init__(self, handler, host='localhost', port=0):
threading.Thread.__init__(self)
self.handler = handler
self.host = host
@@ -16,6 +16,9 @@ class Server(threading.Thread):
def run(self):
sock = socket.socket()
sock.bind((self.host, self.port))
# update port in case self.port = 0
self.port = sock.getsockname()[1]
sock.listen(0)
self.ready_event.set()
self.handler(sock)