- Notifications
You must be signed in to change notification settings - Fork 28
These predefined varables should be customized according to your application by yourself.
MAX_HEADER_SIZE 2048 (deprecated)
It is the maximum buffer size in bytes for each HTTP message header.
MAX_HEADER_SIZE + MAX_BODY_SIZE is the total buffer size in byes for each HTTP message.MAX_BODY_SIZE 4096 (deprecated)
It is the maximum buffer size in bytes for each HTTP message body.
MAX_HEADER_SIZE + MAX_BODY_SIZE is the total buffer size in byes for each HTTP message.MHS_PORT 8000
Default listening port of Micro HTTP Server.
MAX_HTTP_CLIENT 5
The maximum number of clients could connect at the same time.
classHTTPError(Exception):
'''Define an HTTP error exception.'''def__init__(self, message, error=1):
super(HTTPError, self).__init__(message)
self.error=errorDefine HTTP error for exception.
- message: The error message string.
- error: The error number.
classMessage:
'''HTTP message class.'''def__init__(self):
self.Header= []
self.Body=Noneself._Buf=Noneself._index=0Define a tamplate of the HTTP message.
- Header: The header list of HTTP message in unicode.
- Body: The string of the HTTP message in unicode.
- _Buf: It is the real memory buffer which stores the original HTTP message.
- _index: The length of the original HTTP message.
The first 3 elements of the header are the content of start-line. For requst message, they will be "Method", "URI" and "HTTP Version". For response message, they will be "HTTP Version", "Status Code" and "Reason Phrase".
classHTTPServer:
def__init__(self, host="", port=8000):
self.HOST=hostself.PORT=portself.MAX_CLIENT=MAX_HTTP_CLIENT
...
self.sock.bind((self.HOST, self.PORT))
...
# Start server socket listening.self.sock.listen(self.MAX_CLIENT)
...
defRun(self, callback):
...
defRunLoop(self, callback):
whileTrue:
self.Run(callback)Define the Micro HTTP Server class.
- HOST: The server IP. It can be a zero lenth string.
- PORT: The server's listening port.
- MAX_CLIENT: The max number of concurrent clients.
- Run HTTP server
defRun(self, callback):Run will check the server socket and all of interesting client sockets, then parse the requests and send the responses one time.
callback: The callback function after recieves an HTTP request message.
Run HTTP server in forever loop
defRunLoop(self, callback):RunLoop will do the works that HTTPServerRun does forever.
- callback: The same as Run.
It should be
defcallback(req, res):The function and arguments' name could be changed. The callback should not return any variable.
- req: The parsed HTTP request message.
- res: The HTTP response message.
These predefined varables should be customized according to your application by yourself.
STATIC_FILE_FOLDER "static/"
Static files' sub-directory path.
The server application function (SAF) datatype must be the same as callback function in core library.
classRoutes:
defAddRoute(self, method, uri, saf):
...
defDispatch(self, req, res):
...- Add a route
defAddRoute(self, method, uri, saf):Add a request method, URI and the corresponding server application function (SAF) into the route table.
method: HTTP request method.
uri: HTTP request URI.
saf: The server application function (SAF) will be executed when HTTP server gets a matech HTTP request method and URI.
Dispatch HTTP request message
defDispatch(req, res):Dispatch the HTTP method and URI according to the route table.
- req: The parsed HTTP request message.
- res: The HTTP response message.