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.
  1. More informative logging.
  2. A quick wrapper for logging to a file and stdout/stderr.
  3. Trailing-slash redirects on directories.
  4. Optional built-in favicon.ico (to reduce the number of spurious log entries).
  5. Subclass which does not allow any directory lists or redirecting to /index.htm[l] .
  6. Subclass which does not allow any directory lists, but does automatic redirection to /index.htm[l] .
  7. Doesn’t try to open reserved names on Windows.
  8. 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()
class HardwareObjects.AsyncHttpServer.ParseHeaders(infile, *args)

Bases: dict

get(key, default='')
getheader(key, default='')
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)

Project Versions

Previous topic

AbstractMultiCollect Module

Next topic

BeamlineSetup Module

This Page