Packet Tracer 7.2 - Real HTTP server using SBC device

4 1 1 1 1 1 Rating 4.00 (1 Vote)

Real HTTP server API in Packet Tracer 7.2

Packet Tracer 7.2 introduced the capability to create a real HTTP server accessible from the outside of the Packet Tracer emulation environment. This capability is only available on the SBC IoT device.

HTTP server can be programmed using javascript, python, or the visual programming language

Javascript API

The following javascript functions have been added in Packet Tracer 7.2 (SBC device only) to support real HTTP server. The HTTP is able to listen on a custom TCP port and to respond with text/plain or any other content-type (file, ...).

Function

Return Type

Description 

Example 

HTTPServer.route(path, method, callback); 

N/A

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

HTTPServer.route("/hello", function(url, response) { 

  response.send("world"); 

});



HTTPServer.route("/*", function(url, response) { 

  response.send("hi"); 

});

HTTPServer.start(port) 

boolean

Starts listening on port. 

HTTPServer.start(80); 

HTTPServer.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()

 

Use the following javascript code to start a real HTTP server listening on TCP port 8080.

var server = new RealHTTPServer();
server.start(8080);

 

Python API

The Python package is real HTTP. 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()