socktools.webwrap - web.py WSGI wrapper

Often it makes sense to run an internal webserver for administration purposes, it also makes sense to use the existing implementation.

This module provides a simple wrapper around web.py inside eventlet. The wrapper is intended to be used inside of daemon processses but is useful in other places too.

class socktools.webwrap.WebWrap(urls={}, physical_sock=None, listen_on=('0.0.0.0', 8080))[source]

Bases: object

A light wrapper around web.py and eventlet.wsgi

This class is intended for providing a simple embedded webserver inside of daemon processes (or elsewhere).

After configuring URLs, simply run start_server() to start it up.

Warning

You must not use a physical socket from one of the socktools classes here.

Keyword Arguments:
 
  • urls (dict) – Map URL paths to URL handlers, see web.py documentation
  • physical_sock (socket.socket) – If not None, the existing physical socket to listen on
  • listen_on (tuple) – A tuple of (interface_ip,port) that the webserver should listen on
get_default_urls()[source]

Return the default URL mappings

Override this in your app. The default implementation returns a single URL for / which returns a bit of static text

Returns:a dict mapping web.py URLs to web.py URL handlers
Return type:dict
get_url_map()[source]

Generates a URL map for this webserver

To integrate better with the rest of socktools, WebWrap offers a get_default_urls() method that returns a dict.

Unfortunately web.py requires a tuple, so this method generates that tuple.

Returns:the web.py compatible URL map
Return type:tuple
get_wsgifunc()[source]

Return a WSGI-compatible function

In the default implementation this returns the WSGI function from a web.py app

server_thread()[source]

Used internally - server greenlet

start_server()[source]

Starts the webserver in a new greenlet

Call this after setting up URLs etc and the webserver will start answering requests.

socktools.webwrap.static_page(page_content, mime_type='text/html')[source]

Generate and return a URL handler for static content

This function uses a bit of metaprogramming black magic and returns a web.py URL handler for arbitrary static content

Returns:the URL handler
Return type:object