coapy.httputil

To simplify comparison between CoAP and HTTP, these classes are provided to support resources with an HTTP interface.

copyright:Copyright 2013, Peter A. Bigot
license:Apache-2.0
class coapy.httputil.HTTPServer(server_address, RequestHandlerClass, bind_and_activate=True)[source]

Bases: BaseHTTPServer.HTTPServer

Modifications required to provide the server development interface we want.

make_uri(path, query=u'', fragment=u'')[source]

Create an absolute URI for something hosted on this server.

Needed for things like a Location header.

Note

This function will not return a usable URI if BaseHTTPServer.HTTPServer.socket is bound to a wildcard address.

class coapy.httputil.HTTPRequestHandler(request, client_address, server)[source]

Bases: BaseHTTPServer.BaseHTTPRequestHandler

Modifications required to provide the server development interface we want.

This version inverts control relative to the Python standard web server hierarchy: Rather than have the supported methods defined by the generic handler, we follow REST principles and delegate the selection of supported methods to the resources. A HTTPRequestHandler class retains in _Resources a map from path prefixes to instances of HTTPResource which in turn provide the methods that are appropriate to that resource. Processing is delegated to the registered resource whose HTTPResource.path is the longest segment prefix of the request URI path.

classmethod add_resource(resource)[source]

Add resource to the registry for this class at resource.path. This function is automatically invoked in the constructor for HTTPResource.

handle_one_request()[source]

Handle a single HTTP request.

Replace parts of BaseHTTPServer.BaseHTTPRequestHandler.handle_one_request() to identify a HTTPResource instance using the path part of the URI, and delegate all processing (including checking for supported methods) to that resource. If no segment prefix of path is associated with a registered resource a 404 error is returned to the client.

This function assigns split_uri.

Note

Resource lookup uses the the path part of the URI, exclusive of any query or fragment components that may also be present in path.

classmethod lookup_resource(path)[source]

Find the best registered resource at path.

path is a slash-separated hierarchy of path segments. The registered resource for which HTTPResource.path matches a segmented prefix of path is returned. If no prefix matches, None is returned.

classmethod remove_resource(resource)[source]

Remove resource from the registry.

split_uri = None

The results of invoking urlparse.urlsplit() on path.

class coapy.httputil.HTTPResource(path, handler_class=<class coapy.httputil.HTTPRequestHandler at 0x40b6c18>)[source]

Bases: object

Class supporting a resource with specific methods.

Instances of this class are delegates from HTTPRequestHandler (or a subclass). The appropriate resource is identified by a segment prefix of the path. do_FOO methods are implemented in the resource rather than in the request handler.

path is the absolute path to the resource. handler_class is (the subclass of) HTTPRequestHandler into which the created resource will be registered at path.

Note that the architectural model supports multiple instances of a resource class at different paths.

do_GET(request, head_only=False)[source]

Stub for a GET method.

request is the instance of HTTPRequestHandler that has the request-specific data and ability to communicate results to the client. head_only is False normally, but may be set to True to allow this function to also implement the HEAD method: the implementation is responsible for eliding the body of the response in that case.

do_HEAD(request)[source]

Default implementation delegates to do_GET() with head_only=True.

handle_request(request)[source]

Process a single request.

The command from the request is append to do_ and the resulting method is invoked to execute the operation. If the command is not implemented, a 501 Unsupported Method error is returned to the client.

handler_class[source]

A read-only property providing the subclass of HTTPRequestHandler within which this resource is registered.

path[source]

A read-only property specifying the prefix under which this resource is registered within handler_class.

Previous topic

coapy.util

Next topic

tests.support

This Page