A very minimal HTTP server
Project description
picohttp - A minimal HTTP server
This is an HTTP 1.0 server framework that is useful for supporting applications where the behavior is constrained and relatively simple.
An application can define a server that listens for HTTP requests on a designated port, parses the request, and passes the request to a handler that is implemented by the application. The request handler can access the parsed elements of the request and pass elements to the response. When the handler returns, the server builds the HTTP response and sends it to the client.
The TCP connection from a client is closed after the response is sent. Persistent HTTP 1.1 connections are not supported. Every request is handled in a separate thread unless the server is instantiated with threads=False. The server will start immediately unless it is instantiated with start=False, in which case the start() function must be called. When the server starts it will block indefinitely unless it is instantiated with block=False.
The server does not attempt to validate any of the elements of the request or response, it just passes them to and from the request handler. Any method, header, or status code may be used whether it is valid or not. It is the responsibility of the request handler to perform any validation that may be required. The server does require a valid Content-Length header if there is data present in the body of the request, otherwise the data will be ignored.
If the request handler does not assign a value to the response status code, it is assumed to be 200. A Content-Length header is automatically added in the response to indicate the length of the response data.
Specification
class HttpServer(port, handler, args=(), threads=True, start=True, block=True)
port
The port to listen on.
handler
The function defined to be the request handler.
args
A tuple containing optional additional arguments to be passed to the request handler
threads
If True handle each request in a separate thread, otherwise process them sequentially.
start
If True immediately start the server, otherwise the start() function must be called.
block
If True the server will block indefinitely when it is started, otherwise the function starting it will return.
start()
Start the server if it was instantiated with start=False.
class HttpRequest(method, path, query, protocol, headers, data)
method
The HTTP method contained in the request.
path
A list of strings that were parsed from the hier-part of the HTTP request URI.
query
A dictionary containing keywords and values parsed from the query part of the HTTP request URI.
protocol
The HTTP protocol version contained in the request.
headers
A dictionary containing keywords and values of the request headers.
data
The data contained in the request body if present.
class HttpResponse(protocol, status, headers, data)
protocol
The response HTTP protocol version (Default: HTTP/1.0)
status
The response status code (Default: 200)
headers
A dictionary containing additional response headers.
data
Data to be sent in the response body.
The application must define a function to handle requests that is passed to the HttpServer object when it is instantiated. Two positional arguments are required. Additional arguments may be defined and the values for these are passed to the HttpServer object.
def requestHandler(request, response[, args, ...])
request
A HttpRequest object containing elements parsed from an HTTP request.
response
A HttpResponse object that the request handler will populate to form the HTTP response.
Examples
This is a very simple application.
from picohttp import *
def requestHandler(request, response):
if request.path[0] == "hello":
response.data = "Hello world!"
elif request.path[0] == "goodbye":
response.data = "Goodbye cruel world"
else:
response.status = 404
httpServer = HttpServer(8080, requestHandler)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file picohttp-0.0.5.tar.gz
.
File metadata
- Download URL: picohttp-0.0.5.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd7e91958cbdfe03a0dbfc3f04f7175eb416baa4f36cea8e5079316268da01e4 |
|
MD5 | 840d69eaf0827645a2a6970917898906 |
|
BLAKE2b-256 | 5efd890c1638939c271f9c337e767da74bc5b47a9221262a23d78e0eb7c3ee6f |
File details
Details for the file picohttp-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: picohttp-0.0.5-py3-none-any.whl
- Upload date:
- Size: 17.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee212b08c7783451618e86a23ac96f5aa55ab759487378761f3d4e39df00c5d5 |
|
MD5 | ad1ef8f84b1c095ae2300ee676de2f6a |
|
BLAKE2b-256 | d8d3f0fb2ac02ff69b7b20f62a995587be3a9595d2576c99ee5ef6ddb747aa16 |