AsyncHttpServer Module
- Get an updated version of this server from:
- http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440665
- Original pseudo-async* version by Pierre Quentel, available from
- http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259148
- It would only be async while reading requests, it would switch to a
synchronous write to a socket when writing a response to a socket. Ick.
Features this version offers:
1. Uses asynchronous reading and writing to/from sockets all the time.
2. Performs sectioned reads from on-disk files, allowing for serving files of
unlimited size.
- More informative logging.
- A quick wrapper for logging to a file and stdout/stderr.
- Trailing-slash redirects on directories.
- Optional built-in favicon.ico (to reduce the number of spurious log
entries).
- Subclass which does not allow any directory lists or redirecting to
/index.htm[l] .
- Subclass which does not allow any directory lists, but does automatic
redirection to /index.htm[l] .
- Doesn’t try to open reserved names on Windows.
- Has a tuning mechanism to change buffer performance depending on small
or large files.
For most people, one can run this from the command line and get a reasonably
functioning web server with minor issue.
I benchmarked this version in my personal projects folder, which contains
around a gig of files, sizes ranging from a few kilobytes, to 100 megabytes.
I then performed two concurrent “wget -m http://localhost/” calls (from
different paths) on Windows 2k (machine is a P4 2.8ghz with hyperthreading,
1.5 gigs memory, reading from an 80 gig SATA drive, writing to a 120 gig PATA
drive).
On small files, it was able to serve up 15-30 files/second. On larger (10+
meg) files, it was able to serve up at 15+ megs/second (so says adding the
speed reported by wget). The server never broke 7 megs of resident memory, and
tended to hang below 10% processor utilization.
There exists a live host running this web server: nada.ics.uci.edu
-
class HardwareObjects.AsyncHttpServer.ExactFilesAndIndex(conn, addr, server)
Bases: HardwareObjects.AsyncHttpServer.RequestHandler
-
list_directory(path)
-
class HardwareObjects.AsyncHttpServer.OnlyExactFiles(conn, addr, server)
Bases: HardwareObjects.AsyncHttpServer.RequestHandler
-
send_head()
Bases: dict
-
class HardwareObjects.AsyncHttpServer.RequestHandler(conn, addr, server)
Bases: asynchat.async_chat, BaseHTTPServer.BaseHTTPRequestHandler
-
MessageClass
alias of ParseHeaders
-
blocksize = 4096
-
collect_incoming_data(data)
Collect the data arriving on the connexion
-
do_GET()
Begins serving a GET request
-
do_POST()
Begins serving a POST request. The request data must be readable
on a file-like object called self.rfile
-
end_headers()
Send the blank line ending the MIME headers, send the buffered
response and headers on the connection
-
handle_data()
Class to override
-
handle_error()
-
handle_post_data()
Called when a POST request body has been read
-
handle_request_line()
Called when the http request line and headers have been received
-
handle_write()
-
log_message(format, *args)
-
prepare_POST()
Prepare to read the request body
-
protocol_version = 'HTTP/1.1'
-
send_head()
-
send_response(code, message=None)
-
server_version = 'BaseAsyncHTTPServer/.4.1'
-
update_b(fsize)
-
use_buffer = False
-
use_favicon = True
-
writable()
-
class HardwareObjects.AsyncHttpServer.Server(ip, port, handler)
Bases: asyncore.dispatcher
-
handle_accept()
-
HardwareObjects.AsyncHttpServer.popall(self)
-
class HardwareObjects.AsyncHttpServer.to_logfile(fileobj)
-
fl = True
-
write(data)
-
class HardwareObjects.AsyncHttpServer.writewrapper(d, blocksize=4096)
Bases: object
-
write(data)