-
Notifications
You must be signed in to change notification settings - Fork 25
Py API
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.
class HTTPError(Exception):
'''Define an HTTP error exception.'''
def __init__(self, message, error=1):
super(HTTPError, self).__init__(message)
self.error = error
Define HTTP error for exception.
- message: The error message string.
- error: The error number.
class Message:
'''HTTP message class.'''
def __init__(self):
self.Header = []
self.Body = None
self._Buf = None
self._index = 0
Define 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".
class HTTPServer:
def __init__(self, host="", port=8000):
self.HOST = host
self.PORT = port
self.MAX_CLIENT = MAX_HTTP_CLIENT
...
self.sock.bind((self.HOST, self.PORT))
...
# Start server socket listening.
self.sock.listen(self.MAX_CLIENT)
...
def Run(self, callback):
...
def RunLoop(self, callback):
while True:
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
def Run(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
def RunLoop(self, callback):
RunLoop will do the works that HTTPServerRun does forever.
- callback: The same as Run.
It should be
def callback(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.
class Routes:
def AddRoute(self, method, uri, saf):
...
def Dispatch(self, req, res):
...
- Add a route
def AddRoute(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
def Dispatch(req, res):
Dispatch the HTTP method and URI according to the route table.
- req: The parsed HTTP request message.
- res: The HTTP response message.