socktools.http_mixin - toy HTTP implementation

Overview

This is a simple implementation of HTTP for demonstration purposes, if you need a webserver for production use, see webwrap.py.

Python API

This module provides a mixin that gets HTTP requests from sockets, the basis of every webserver. No client implementation is provided - urllib or python-requests serves that purpose just fine.

Although it’d be cool, this does not currently support HTTP over UDP. Use it with a TCP socket. The HTTP support is also very basic at present.

Warning

Avoid using send_msg() on HTTP server sockets, this may cause various subtle and tricky to debug issues. Instead use the request object to reply.

class socktools.http_mixin.HTTPMixin[source]

Bases: object

A mixin for implementing HTTP

Use this to get HTTP requests from the socket

do_real_read(s)[source]

Read a single HTTP request

This method will read a single HTTP request without parsing it and then close the socket. Essentially we just read until the socket closes - which is a bit of a cheat but works.

parse_msg(args)[source]
class socktools.http_mixin.HTTPRequest(verb='GET', path='/', headers={}, client_sock=None, server_sock=None)[source]

Bases: object

Represents an HTTP request

Instances of this class are generated by HTTPMixin and used in the msg_data field.

To respond to the request, use the reply() method.

reply(status=(200, 'OK'), body='', headers={})[source]