Accepts TCP connections on the specified host and port, and invokes the provided handler function for every new connection. Returns the created server socket.
(name, handler, host, port=0, backlog=socket.SOMAXCONN, timeout=None)
| 139 | |
| 140 | |
| 141 | def serve(name, handler, host, port=0, backlog=socket.SOMAXCONN, timeout=None): |
| 142 | """Accepts TCP connections on the specified host and port, and invokes the |
| 143 | provided handler function for every new connection. |
| 144 | |
| 145 | Returns the created server socket. |
| 146 | """ |
| 147 | |
| 148 | assert backlog > 0 |
| 149 | |
| 150 | try: |
| 151 | listener = create_server(host, port, backlog, timeout) |
| 152 | except Exception: # pragma: no cover |
| 153 | log.reraise_exception( |
| 154 | "Error listening for incoming {0} connections on {1}:{2}:", name, host, port |
| 155 | ) |
| 156 | host, port = get_address(listener) |
| 157 | log.info("Listening for incoming {0} connections on {1}:{2}...", name, host, port) |
| 158 | |
| 159 | def accept_worker(): |
| 160 | while True: |
| 161 | try: |
| 162 | sock, address = listener.accept() |
| 163 | other_host, other_port = address[:2] |
| 164 | except (OSError, socket.error): |
| 165 | # Listener socket has been closed. |
| 166 | break |
| 167 | |
| 168 | log.info( |
| 169 | "Accepted incoming {0} connection from {1}:{2}.", |
| 170 | name, |
| 171 | other_host, |
| 172 | other_port, |
| 173 | ) |
| 174 | handler(sock) |
| 175 | |
| 176 | thread = threading.Thread(target=accept_worker) |
| 177 | thread.daemon = True |
| 178 | hide_thread_from_debugger(thread) |
| 179 | thread.start() |
| 180 | |
| 181 | return listener |
nothing calls this directly
no test coverage detected
searching dependent graphs…