1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
7 import SimpleHTTPServer
10 class LocalHTTPServer(object):
11 """Class to start a local HTTP server as a child process."""
13 def __init__(self
, serve_dir
):
14 parent_conn
, child_conn
= multiprocessing
.Pipe()
15 self
.process
= multiprocessing
.Process(target
=_HTTPServerProcess
,
16 args
=(child_conn
, serve_dir
))
18 if parent_conn
.poll(10): # wait 10 seconds
19 self
.port
= parent_conn
.recv()
21 raise Exception('Unable to launch HTTP server.')
23 self
.conn
= parent_conn
26 """Send a message to the child HTTP server process and wait for it to
31 def GetURL(self
, rel_url
):
32 """Get the full url for a file on the local HTTP server.
35 rel_url: A URL fragment to convert to a full URL. For example,
36 GetURL('foobar.baz') -> 'http://localhost:1234/foobar.baz'
38 return 'http://localhost:%d/%s' % (self
.port
, rel_url
)
41 class QuietHTTPRequestHandler(SimpleHTTPServer
.SimpleHTTPRequestHandler
):
42 def log_message(self
, msg_format
, *args
):
46 def _HTTPServerProcess(conn
, serve_dir
):
47 """Run a local httpserver with a randomly-chosen port.
49 This function assumes it is run as a child process using multiprocessing.
52 conn: A connection to the parent process. The child process sends
53 the local port, and waits for a message from the parent to
55 serve_dir: The directory to serve. All files are accessible through
56 http://localhost:<port>/path/to/filename.
61 httpd
= BaseHTTPServer
.HTTPServer(('', 0), QuietHTTPRequestHandler
)
62 conn
.send(httpd
.server_address
[1]) # the chosen port number
63 httpd
.timeout
= 0.5 # seconds
66 httpd
.handle_request()