Python API

The Python package used for creating the HTTPServer object is named "realhttp".

Include the from realhttp import * line at the beginning of your Python code.

Function

Return Type

Description

Example

route(path, callback);

N/A

Sets up a route for path and calls callback when it is requested. Routes also support wildcards using *.

The route function calls the defined function based on the HTTP method and the request URI.

In the following example, the gest_contacts function is called for each GET request to an URI starting with "/contacts/" and the post_data function is called for each POST request to an URI starting with "data". 

server.route("/contacts/*", ["GET"], get_contacts)
server.route("/services/*", ["GET"], get_services)
server.route("/data/*", ["POST"], post_data)

def onRouteHello(url, response):
response.send("hello")
HTTPServer.route("/hello", onRouteHello)



def onRouteAll(url, response):
response.send("world")
HTTPServer.route("/*", onRouteAll)

 

start(port) 

bool

Starts listening on port. 

HTTPServer.start(80)

stop() 

N/A

Stops listening. 

HTTPServer.stop()

 

 

 

 

Response class

 

Passed into the HTTPServer route handler.

 

send(content)

N/A

Sends content back as response.

response.send("hello")

setContentType(type)

N/A

Sets the content type in the response.

response.setContentType("text/plain")

sendFile(filePath)

N/A

Sends a file back as response. The file path is in the device's file manager, not relative to the source code of the current project/script.

response.sendFile("/test.txt")

sendNotFound()

N/A

Sends a file not found as response.

response.sendNotFound()