PYME.ParallelTasks.HTTPDataServer module

Created on Sat Feb 13 18:12:18 2016

@author: david

This is a very simple HTTP server which allows data to be saved to the server using PUT

Security note:

The code as it stands lets any client write arbitrary data to the server. The only concessions we make to security are:

  1. paths are vetted to make sure that they reside under out root (i.e. no escape through .. etc)
  2. we enforce a write-once scheme (if a file exists it can’t be overridden)

This protects against people accidentally or maliciously altering data, or discovering server settings, but leaves us open to denial of service type attacks in which a malicious client could fill up our storage.

THE CODE AS IT STANDS SHOULD ONLY BE USED ON A TRUSTED NETWORK

TODO: Add some form of authentication. Needs to be low overhead (e.g. digest based)

class PYME.ParallelTasks.HTTPDataServer.PYMEHTTPRequestHandler(request, client_address, server)

Bases: SimpleHTTPServer.SimpleHTTPRequestHandler

Attributes

timeout  

Methods

do_GET() Serve a GET request.
do_PUT()
get_status()
list_directory(path) Helper to produce a directory listing (absent index.html).
log_error(format, *args) Log an error.
log_message(format, *args) Log an arbitrary message.
log_request([code, size]) Log an accepted request.
send_error(code[, message]) Send and log an error reply.
send_head() Common code for GET and HEAD commands.
bandwidthTesting = False
do_GET()

Serve a GET request.

do_PUT()
get_status()
list_directory(path)

Helper to produce a directory listing (absent index.html).

Return value is either a file object, or None (indicating an error). In either case, the headers are sent, making the interface the same as for send_head().

log_error(format, *args)

Log an error.

This is called when a request cannot be fulfilled. By default it passes the message on to log_message().

Arguments are the same as for log_message().

XXX This should go to the separate error log.

log_message(format, *args)

Log an arbitrary message.

This is used by all other logging functions. Override it if you have specific logging wishes.

The first argument, FORMAT, is a format string for the message to be logged. If the format string contains any % escapes requiring parameters, they should be specified as subsequent arguments (it’s just like printf!).

The client ip address and current date/time are prefixed to every message.

log_request(code='-', size='-')

Log an accepted request.

This is called by send_response().

logrequests = False
protocol_version = 'HTTP/1.0'
send_error(code, message=None)

Send and log an error reply.

Arguments are the error code, and a detailed message. The detailed message defaults to the short entry matching the response code.

This sends an error response (so it must be called before any output has been generated), logs the error, and finally sends a piece of HTML explaining the error to the user.

send_head()

Common code for GET and HEAD commands.

This sends the response code and MIME headers.

Return value is either a file object (which has to be copied to the outputfile by the caller unless the command was HEAD, and must be closed by the caller under all circumstances), or None, in which case the caller has nothing further to do.

class PYME.ParallelTasks.HTTPDataServer.ThreadedHTTPServer(server_address, RequestHandlerClass, bind_and_activate=True)

Bases: SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer

Handle requests in a separate thread.

Attributes

timeout  

Methods

Constructor. May be extended, do not override.

Attributes

timeout  

Methods

PYME.ParallelTasks.HTTPDataServer.getTextFileLock(filename)
PYME.ParallelTasks.HTTPDataServer.main(protocol='HTTP/1.0')
PYME.ParallelTasks.HTTPDataServer.makedirs_safe(dir)

A safe wrapper for makedirs, which won’t throw an error if the directory already exists. This replicates the functionality of the exists_ok flag in the python 3 version of os.makedirs, but should work with both pytion 2 and python 3.

Parameters:dir : str, directory to be created
class PYME.ParallelTasks.HTTPDataServer.statusPoller(*args, **kwargs)

Bases: threading.Thread

Attributes

daemon A boolean value indicating whether this thread is a daemon thread (True) or not (False).
ident Thread identifier of this thread or None if it has not been started.
name A string used for identification purposes only.

Methods

run()
stop()
run()
stop()
PYME.ParallelTasks.HTTPDataServer.updateStatus()