M2.6.3 - Python-Befehlsreferenz für den "Packet Tracer"
Python API (Quelle: "Packet Tracer" > "Help" > "Contents" > "Internet of Things" > "Python API")
HTTP Client |
Package = http |
||
Function |
Return Type |
Description |
Example |
HTTPClient() |
HTTPClient |
Creates a HTTP Client. |
http = HTTPClient() |
open(url) |
N/A |
Gets an URL. |
http.open(“http://www.cisco.com”) |
stop() |
N/A |
Stops the request. |
http.stop() |
onDone(callback) |
N/A |
Sets the callback for when the request is done. |
def onHTTPDone(status, data): ... http.onDone(onHTTPDone) |
Real HTTP Server (External Network Access) |
Package = realhttp |
|||
Function |
Return Type |
Description |
Example |
|
RealHTTPServer() |
RealHTTPServer |
Creates a Real HTTP Server. |
http = RealHTTPServer()
|
|
start( port ) |
N/A |
Start server listening on port. |
http.start( 8765 ) |
|
stop |
N/A |
Stop server listening. |
http.stop() |
|
isListening |
boolean |
Returns the listening status of the server. |
if
http.isListening() : |
|
route( url-pattern, request-types, callback, context=None) |
N/A |
Adds callback to be run for all request-types matching url-pattern. callback: function ( context, request, reply); url-pattern: string |
def
on_get_files( context, request, reply ): |
|
websocket( url-pattern, callback) | N/A |
Adds callback to be run for incoming websocket connections matching url-pattern. callback:
function (client);
url-pattern: string |
def callback( client ):
server.websocket("/ws", callback)
|
|
|
|
|
|
|
RealHTTPServerRequest |
|
Request object passed to routing callbacks. |
|
|
headers |
object |
Returns object containing request headers. |
headers =
request.headers |
|
method |
string |
Returns request's method (“GET”, etc.) |
if
request.method() == “POST”: |
|
body |
string |
Returns the body of the request. |
request.body(); |
|
url |
string |
Returns requested URL. |
request.url(); |
|
ip |
string |
Returns IP address of the request origin. |
if
request.ip() == “1.1.1.1”: |
|
|
|
|
|
|
RealHTTPServerResponse |
|
Response object passed to routing callbacks. |
|
|
setStatus( code ) |
N/A |
Sets response status to code. |
|
|
setHeaders( headers ) |
N/A |
Sets response headers in bulk. Object properties define header
names and values. |
response.setHeaders({ |
|
addHeader( name, value ) |
N/A |
Sets header name to
value. |
response.addHeader( “Cookie”, “Group=inner” ) |
|
appendContent( content ) |
N/A |
Appends content to response body. content: string |
response.appendContent( “more stuff“ ) |
|
resetContent |
N/A |
Resets the body of the response to empty. |
response.resetContent() |
|
end |
N/A |
Initiates network transfer for the response. |
response.setContent(
“Hello, world!” ) response.setToPath(
“my-file.txt” ) |
|
setContent( content ) |
N/A |
Sets the body of the response to content. content: string |
response.setContent( “Hello, world!” ) |
|
setToFile( path ) |
N/A |
Sets content of the response to the contents of the file at path. The path must be local to the device running the server. path: string |
response.setToFile( “intro.html” ) |
|
setToNotFound |
N/A |
Canned response for when a requested resource can not be located. |
response.setToNoFound() |
|
send( content ) |
N/A |
Sends content as plain text. content: string |
response.send( “Hello, world!” ) |
|
setContentType( type ) |
N/A |
Sets Content-Type header to type. type: string |
response.setContentType( “text/html” ) |
|
sendFile( path ) |
N/A |
Sends the contents of the file at path. path: file path local to the device running the server. |
response.send( “my-file.txt” ) |
|
sendNotFound |
N/A |
Sends canned response for when a requested resource can not be located. |
response.sendNotFound() |
Real HTTP Client
|
Package = realhttp |
||
Function |
Return Type |
Description |
Example |
RealHTTPClient() |
RealHTTPClient |
Creates a Real HTTP Client. |
http = RealHTTPClient() |
get(url) |
N/A |
Gets an URL. |
http.get(“http://www.cisco.com”) |
post(url, data) |
N/A |
Posts data to an URL. data can be a string or a dictionary; if dictionary, it will be URL-encoded into the body. |
http.post(url, {"num":1, "str":"hello"}) |
put(url, data) |
N/A |
Puts data to an URL. data can be a string or a dictionary; if dictionary, it will be URL-encoded into the body. |
http.put(url, {"num":1, "str":"hello"}) |
deleteResource(url) |
N/A |
Sends a delete to an URL. |
http.deleteResource(url) |
getWithHeader(url, header) |
N/A |
Gets an URL with custom header fields as a dictionary. |
http.getWithHeader("https://api.ciscospark.com/v1/people/me", {"Authorization": "Bearer xxyyzz"}) |
postWithHeader(url, data, header) |
N/A |
Posts data to an URL with custom header fields as a dictionary. data can be a string or a dictionary; if dictionary and custom header field has "application/json" as the "content-type", it will be json-encoded, otherwise it will be URL-encoded into the body. |
http.postWithHeader("https://api.ciscospark.com/v1/messages", {"toPersonId": "722bb271-d7ca-4bce-a9e3-471e4412fa77", "text": "Hi Sparky"}, {"Authorization": "Bearer xxyyzz", "Content-Type": "application/json"}) |
putWithHeader(url, data, header) |
N/A |
Puts data to an URL with custom header fields as a dictionary. data can be a string or a dictionary; if dictionary and custom header field has "application/json" as the "content-type", it will be json-encoded, otherwise it will be URL-encoded into the body. |
http.putWithHeader("https://api.ciscospark.com/v1/rooms/xxyyzz", {"title": "New room name"}, {"Authorization": "Bearer xxyyzz"}) |
deleteResourceWithHeader(url, header) |
N/A |
Sends a delete to an URL with custom header fields as a dictionary. |
http.deleteResourceWithHeader("https://api.ciscospark.com/v1/messages/xxyyzz", {"Authorization": "Bearer xxyyzz"}) |
onDone(callback) |
N/A |
Sets the callback for when the request is done. replyHeader is a dictionary of header fields in the reply. It is added to 7.1 and is optional. |
def onHTTPDone(status, data, replyHeader): print(data) ... http.onDone(onHTTPDone) |