pyserv package

Submodules

Module contents

Simple HTTP server classes with GET path rewriting and request/header logging. Now includes a reference WSGI server and tftpdaemon script.

class pyserv.GetHandler(*args, directory=None, **kwargs)[source]

Bases: SimpleHTTPRequestHandler

Munge the incoming request path from Dialog OTA. Runs urlparse on the url and updates the GET handler path. We also log the result.

do_GET()[source]

Serve a GET request.

log_message(format, *args)[source]

We need a custom log handler, otherwise Get message goes to sys.stdout only.

class pyserv.GetServer(iface, port, directory='.')[source]

Bases: Thread

Threaded wrapper class for custom ThreadingHTTPServer instance.

Usage:

s = GetServer('', 8080)
s.start()
s.stop()
run()[source]

Start main server thread

stop()[source]

Stop main server thread

class pyserv.GetServerWSGI(flask_app, port, is_flask=False, validate=False)[source]

Bases: Thread

Threaded wrapper class for custom flask WSGIServer instance.

Usage:

s = GetServerWSGI(my_app, 8080, is_flask=True, validate=False)
s.start()
s.stop()
run()[source]

Start main server thread

stop()[source]

Stop main server thread

class pyserv.RepeatTimer(interval, function, *args, **kwargs)[source]

Bases: object

A non-blocking timer thread to execute a user func repeatedly. Usage:

def hello(name):
    print(f"Hello {name}")

rt = RepeatTimer(1, hello, "World")  # it auto-starts
try:
    sleep(5)  # run other stuff
finally:
    rt.stop()  # best in a try/finally block

Author: https://stackoverflow.com/a/38317060/14874218

Parameters:
  • interval (int | float)

  • function (Callable)

start()[source]

Safely (re)start thread timer.

stop()[source]

Safely stop thread timer.

pyserv.munge_url(ota_url)[source]

Parse the url sent by OTA command for file path and host string.

Parameters:

ota_url – (possibly) broken GET path

Return tuple:

netloc and path from urlparse