server.py
This file
Path
testserver/server.py
107 lines · 3.1 KiB · Python
Last changed by
Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
History of this fileTake it
Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
lines 1–2why, one step back1#!/usr/bin/python2Allow main thread to block testserver before closingBraulio Valdivielso Martínezbefore adoption6551c29
lines 3–4why, one step back3import threading 4import socketAdded a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
line 5why, one step back5import selectRenamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
line 6why, one step back6Allow main thread to block testserver before closingBraulio Valdivielso Martínezbefore adoption6551c29
line 7why, one step back7Fixed styleBraulio Valdivielso Martínezbefore adoptionfa91b0a
lines 8–9why, one step back8def consume_socket_content(sock, timeout=0.5):9 chunks = 65536Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
lines 10–11why, one step back10 content = ""11 more_to_read = select.select([sock], [], [], timeout)[0]Allow main thread to block testserver before closingBraulio Valdivielso Martínezbefore adoption6551c29
line 12why, one step back12Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
line 13why, one step back13 while more_to_read:Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
line 14why, one step back14 new_content = sock.recv(chunks)Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
line 15why, one step back15Fixed styleBraulio Valdivielso Martínezbefore adoptionfa91b0a
lines 16–17why, one step back16 if not new_content:17 breakAdded a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
line 18why, one step back18Fixed styleBraulio Valdivielso Martínezbefore adoptionfa91b0a
lines 19–21why, one step back19 content += new_content 20 # stop reading if no new data is received for a while 21 more_to_read = select.select([sock], [], [], timeout)[0] Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
lines 22–24why, one step back2223 return content24Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
lines 25–26why, one step back2526Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
line 27why, one step back27class Server(threading.Thread):Made TestServer.Server listen on an arbitrary open port by defaultBraulio Valdivielso Martínezbefore adoptionab0f063
line 28why, one step back28 """ Dummy server using for unit testing """Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
line 29why, one step back29 WAIT_EVENT_TIMEOUT = 5Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
line 30why, one step back30Allow main thread to block testserver before closingBraulio Valdivielso Martínezbefore adoption6551c29
line 31why, one step back31 def __init__(self, handler, host='localhost', port=0, requests_to_handle=1, wait_to_close_event=None):Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
line 32why, one step back32 threading.Thread.__init__(self)Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
line 33why, one step back33Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
line 34why, one step back34 self.handler = handlerAdded a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
lines 35–36why, one step back35 self.handler_results = []36Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
lines 37–38why, one step back37 self.host = host38 self.port = portAllow main thread to block testserver before closingBraulio Valdivielso Martínezbefore adoption6551c29
lines 39–41why, one step back39 self.requests_to_handle = requests_to_handle40 41 self.wait_to_close_event = wait_to_close_eventRenamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
lines 42–44why, one step back42 self.ready_event = threading.Event()43 self.stop_event = threading.Event()44Added basic_response_server classmethod and tested itBraulio Valdivielso Martínezbefore adoption11ad502
line 45why, one step back45 @classmethodAdded a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
line 46why, one step back46 def text_response_server(cls, text, request_timeout=0.5, **kwargs):Added text_response_server classmethod to TestServer and updated basic_response_server to use itBraulio Valdivielso Martínezbefore adoption3b2a489
line 47why, one step back47 def text_response_handler(sock):Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
line 48why, one step back48 request_content = consume_socket_content(sock, timeout=request_timeout)Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
line 49why, one step back49 sock.send(text.encode('utf-8'))Added basic_response_server classmethod and tested itBraulio Valdivielso Martínezbefore adoption11ad502
line 50why, one step back50Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
lines 51–53why, one step back51 return request_content5253Added text_response_server classmethod to TestServer and updated basic_response_server to use itBraulio Valdivielso Martínezbefore adoption3b2a489
line 54why, one step back54 server = Server(text_response_handler, **kwargs)Added basic_response_server classmethod and tested itBraulio Valdivielso Martínezbefore adoption11ad502
lines 55–56why, one step back55 return server56Added text_response_server classmethod to TestServer and updated basic_response_server to use itBraulio Valdivielso Martínezbefore adoption3b2a489
lines 57–63why, one step back57 @classmethod58 def basic_response_server(cls, **kwargs):59 server = cls.text_response_server(60 "HTTP/1.1 200 OK\r\n" +61 "Content-Length: 0\r\n\r\n", **kwargs62 )63 return serverAllow main thread to block testserver before closingBraulio Valdivielso Martínezbefore adoption6551c29
line 64why, one step back64Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
line 65why, one step back65 def run(self):Make exceptions in the testserver.Server thread not block the main threadBraulio Valdivielso Martínezbefore adoption2ad3f07
lines 66–70why, one step back66 try:67 sock = self._create_socket_and_bind()68 # in case self.port = 069 self.port = sock.getsockname()[1]70 self.ready_event.set()Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
lines 71–74why, one step back71 self._handle_requests(sock)7273 if self.wait_to_close_event:74 self.wait_to_close_event.wait(self.WAIT_EVENT_TIMEOUT)Make exceptions in the testserver.Server thread not block the main threadBraulio Valdivielso Martínezbefore adoption2ad3f07
lines 75–77why, one step back75 finally:76 self.ready_event.set() # just in case of exception77 sock.close()Make server set stop_event right after closing its socketBraulio Valdivielso Martínezbefore adoptionf7917c3
line 78why, one step back78 self.stop_event.set()Make exceptions in the testserver.Server thread not block the main threadBraulio Valdivielso Martínezbefore adoption2ad3f07
lines 79–80why, one step back7980 def _create_socket_and_bind(self):Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
lines 81–83why, one step back81 sock = socket.socket()82 sock.bind((self.host, self.port))83 sock.listen(0)Make exceptions in the testserver.Server thread not block the main threadBraulio Valdivielso Martínezbefore adoption2ad3f07
line 84why, one step back84 return sockRenamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
line 85why, one step back85Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
line 86why, one step back86 def _handle_requests(self, server_sock):Allow main thread to block testserver before closingBraulio Valdivielso Martínezbefore adoption6551c29
lines 87–88why, one step back87 for _ in range(self.requests_to_handle):88 sock = server_sock.accept()[0]Added a way for TestServer request handlers to store data for later retrieval by the main thread (actual requests, for example)Braulio Valdivielso Martínezbefore adoption3eeea37
lines 89–91why, one step back89 handler_result = self.handler(sock)9091 self.handler_results.append(handler_result)Allow main thread to block testserver before closingBraulio Valdivielso Martínezbefore adoption6551c29
line 92why, one step back92 Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
lines 93–94why, one step back93 def __enter__(self):94 self.start()Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
line 95why, one step back95 self.ready_event.wait(self.WAIT_EVENT_TIMEOUT)Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
lines 96–99why, one step back96 return self.host, self.port97 98 def __exit__(self, exc_type, exc_value, traceback):99 if exc_type is None:Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
line 100why, one step back100 self.stop_event.wait(self.WAIT_EVENT_TIMEOUT)Avoid server from blocking if an exception is found in the main threadBraulio Valdivielso Martínezbefore adoption5ecf789
lines 101–102why, one step back101 else:102 if self.wait_to_close_event:Fixed encoding issues, added timeouts to event waits and refactored ServerBraulio Valdivielso Martínezbefore adoption9ac5a6e
lines 103–104why, one step back103 # avoid server from waiting for event timeouts 104 # if an exception is found in the main thread Avoid server from blocking if an exception is found in the main threadBraulio Valdivielso Martínezbefore adoption5ecf789
line 105why, one step back105 self.wait_to_close_event.set() Renamed dummyserver to testserverBraulio Valdivielso Martínezbefore adoption47e96c4
lines 106–107why, one step back106 return False # allow exceptions to propagate 107