server.py
This file
Path
tests/testserver/server.py
103 lines · 3.0 KiB · Python
Last changed by
Leave two lines of separation between top level definitionsBraulio Valdivielso Martínezbefore adoption66c52c3
History of this fileTake it
Moved testserver to tests/Braulio Valdivielso Martínezbefore adoptionca35c84
lines 1–4why, one step back1import threading 2import socket3import select4Leave two lines of separation between top level definitionsBraulio Valdivielso Martínezbefore adoption66c52c3
line 5why, one step back5Moved testserver to tests/Braulio Valdivielso Martínezbefore adoptionca35c84
lines 6–17why, one step back6def consume_socket_content(sock, timeout=0.5):7 chunks = 655368 content = ""9 more_to_read = select.select([sock], [], [], timeout)[0]1011 while more_to_read:12 new_content = sock.recv(chunks)1314 if not new_content:15 break1617 content += new_content Fixed style issuesBraulio Valdivielso Martínezbefore adoption0d1a779
line 18why, one step back18 # stop reading if no new data is received for a whileMoved testserver to tests/Braulio Valdivielso Martínezbefore adoptionca35c84
lines 19–21why, one step back19 more_to_read = select.select([sock], [], [], timeout)[0] 2021 return contentLeave two lines of separation between top level definitionsBraulio Valdivielso Martínezbefore adoption66c52c3
line 22why, one step back22Moved testserver to tests/Braulio Valdivielso Martínezbefore adoptionca35c84
lines 23–24why, one step back2324class Server(threading.Thread):Fixed style issuesBraulio Valdivielso Martínezbefore adoption0d1a779
line 25why, one step back25 """Dummy server using for unit testing"""Moved testserver to tests/Braulio Valdivielso Martínezbefore adoptionca35c84
lines 26–28why, one step back26 WAIT_EVENT_TIMEOUT = 52728 def __init__(self, handler, host='localhost', port=0, requests_to_handle=1, wait_to_close_event=None):Fixed style issuesBraulio Valdivielso Martínezbefore adoption0d1a779
line 29why, one step back29 super(Server, self).__init__()Moved testserver to tests/Braulio Valdivielso Martínezbefore adoptionca35c84
lines 30–50why, one step back3031 self.handler = handler32 self.handler_results = []3334 self.host = host35 self.port = port36 self.requests_to_handle = requests_to_handle37 38 self.wait_to_close_event = wait_to_close_event39 self.ready_event = threading.Event()40 self.stop_event = threading.Event()4142 @classmethod43 def text_response_server(cls, text, request_timeout=0.5, **kwargs):44 def text_response_handler(sock):45 request_content = consume_socket_content(sock, timeout=request_timeout)46 sock.send(text.encode('utf-8'))4748 return request_content4950Fixed style issuesBraulio Valdivielso Martínezbefore adoption0d1a779
line 51why, one step back51 return Server(text_response_handler, **kwargs)Moved testserver to tests/Braulio Valdivielso Martínezbefore adoptionca35c84
lines 52–54why, one step back5253 @classmethod54 def basic_response_server(cls, **kwargs):Fixed style issuesBraulio Valdivielso Martínezbefore adoption0d1a779
line 55why, one step back55 return cls.text_response_server(Moved testserver to tests/Braulio Valdivielso Martínezbefore adoptionca35c84
line 56why, one step back56 "HTTP/1.1 200 OK\r\n" +Fixed style issuesBraulio Valdivielso Martínezbefore adoption0d1a779
lines 57–58why, one step back57 "Content-Length: 0\r\n\r\n", 58 **kwargsMoved testserver to tests/Braulio Valdivielso Martínezbefore adoptionca35c84
lines 59–103why, one step back59 )6061 def run(self):62 try:63 sock = self._create_socket_and_bind()64 # in case self.port = 065 self.port = sock.getsockname()[1]66 self.ready_event.set()67 self._handle_requests(sock)6869 if self.wait_to_close_event:70 self.wait_to_close_event.wait(self.WAIT_EVENT_TIMEOUT)71 finally:72 self.ready_event.set() # just in case of exception73 sock.close()74 self.stop_event.set()7576 def _create_socket_and_bind(self):77 sock = socket.socket()78 sock.bind((self.host, self.port))79 sock.listen(0)80 return sock8182 def _handle_requests(self, server_sock):83 for _ in range(self.requests_to_handle):84 sock = server_sock.accept()[0]85 handler_result = self.handler(sock)8687 self.handler_results.append(handler_result)88 89 def __enter__(self):90 self.start()91 self.ready_event.wait(self.WAIT_EVENT_TIMEOUT)92 return self.host, self.port93 94 def __exit__(self, exc_type, exc_value, traceback):95 if exc_type is None:96 self.stop_event.wait(self.WAIT_EVENT_TIMEOUT)97 else:98 if self.wait_to_close_event:99 # avoid server from waiting for event timeouts 100 # if an exception is found in the main thread 101 self.wait_to_close_event.set() 102 return False # allow exceptions to propagate 103