Run a local httpserver with the given port or an ephemeral port. This function assumes it is run as a child process using multiprocessing. Args: conn: A connection to the parent process. The child process sends the local port, and waits for a message from the parent to stop
(conn, dirname, port, server_kwargs)
| 239 | |
| 240 | |
| 241 | def _HTTPServerProcess(conn, dirname, port, server_kwargs): |
| 242 | """Run a local httpserver with the given port or an ephemeral port. |
| 243 | |
| 244 | This function assumes it is run as a child process using multiprocessing. |
| 245 | |
| 246 | Args: |
| 247 | conn: A connection to the parent process. The child process sends |
| 248 | the local port, and waits for a message from the parent to |
| 249 | stop serving. It also sends a "result" back to the parent -- this can |
| 250 | be used to allow a client-side test to notify the server of results. |
| 251 | dirname: The directory to serve. All files are accessible through |
| 252 | http://localhost:<port>/path/to/filename. |
| 253 | port: The port to serve on. If 0, an ephemeral port will be chosen. |
| 254 | server_kwargs: A dict that will be passed as kwargs to the server. |
| 255 | """ |
| 256 | try: |
| 257 | os.chdir(dirname) |
| 258 | httpd = PluggableHTTPServer(('', port), PluggableHTTPRequestHandler, |
| 259 | **server_kwargs) |
| 260 | except socket.error as e: |
| 261 | sys.stderr.write('Error creating HTTPServer: %s\n' % e) |
| 262 | sys.exit(1) |
| 263 | |
| 264 | try: |
| 265 | conn.send(httpd.server_address[1]) # the chosen port number |
| 266 | httpd.timeout = 0.5 # seconds |
| 267 | while httpd.running: |
| 268 | # Flush output for MSVS Add-In. |
| 269 | sys.stdout.flush() |
| 270 | sys.stderr.flush() |
| 271 | httpd.handle_request() |
| 272 | if conn.poll(): |
| 273 | httpd.running = conn.recv() |
| 274 | except KeyboardInterrupt: |
| 275 | pass |
| 276 | finally: |
| 277 | conn.send(httpd.result) |
| 278 | conn.close() |
| 279 | |
| 280 | |
| 281 | def main(args): |
nothing calls this directly
no test coverage detected