Python API for Third-Party Libraries
urequests
– Network Request Module
urequests
– Network Request ModuleFunction
urequests.request(method, url, data=None, json=None, headers={})
Send a network request, it will block the response data returned to the network, parameters:
method method of establishing a network request. e.g.
HEAD
,GET
,POST
,PUT
,PATCH
,DELETE
.url URL of the network request.
data (optional), a dictionary, tuple list [(key, value)] (will be form coded), byte or class file object sent in the request body.
json (optional), json data sent in the request body.
headers (optional), HTTP header dictionary to be sent with the request.
urequests.head(url, **kw)
Send a HEAD
request, the return type is the response of the request, parameters:
url URL of the network request.
**kw request optional parameters.
urequests.get(url, **kw)
Send a GET
request, the return type is the response of the request, parameters:
url URL of the network request.
**kw request optional parameters.
urequests.post(url, **kw)
Send a POST
request, the return type is the response of the request, parameters:
url URL of the network request.
**kw request optional parameters.
urequests.put(url, **kw)
Send a PUT
request, the return type is the response of the request, parameters:
url URL of the network request.
**kw request optional parameters.
urequests.patch(url, **kw)
Send a PATCH
request, the return type is the response of the request, parameters:
url URL of the network request.
**kw request optional parameters.
urequests.delete(url, **kw)
Send a DELETE
request, the return type is the response of the request, parameters:
url URL of the network request.
**kw request optional parameters.
Sample Code:
Sample Code:2
Sample Code:3
mqtt
– Message Queue Telemetry Transmission
mqtt
– Message Queue Telemetry TransmissionClass
class mqtt.MQTTClient(client_id, server, port=0, user=None, password=None, keepalive=0, ssl=False, ssl_params={})
Instantiating the interface object of MQTT client, parameters:
client_id the unique client id string used when connecting to the broker. If client_id is zero length or None, then one will be randomly generated. In this case, the parameter
clean_session
of theconnect
function must beTrue
.server The host name or IP address of the remote server.
port (optional), the network port of the server host to connect to. The default port number is 1883. Please note that the default port number of the MQTT over SSL/TLS is 8833.
user (optional), the username registered on the server.
password (optional), the password registered on the server.
keepalive (optional), the client’s keepalive time out value. Default is 60 s.
ssl (optional), whether enable the SSL/TLS support.
ssl_params (optional), SSL/TLS parameter.
connect(clean_session=True)
Connect the client to the server, this is a blocking function, parameters:
clean_session a boolean that determines the client type. If
True
, the broker will remove all information about this client when it disconnects. IfFalse
, the client is a durable client and subscription information and queued messages will be retained when the client disconnects.
reconnect()
Reconnect to the server using the details provided previously. You must call connect
before calling this function.
disconnect()
Disconnect from the server.
ping()
Test the connectivity between the server and client.
set_last_will(topic, msg, retain=False, qos=0)
Set the will to be sent to the server. If the client disconnects without calling disconnect()
, the server will post a message on its behalf, parameters:
topic The topic of the will post.
msg A will message to send.
retain If set to
True
, the will message will be set to the last knowngood/reserved message
for the topic.qos Is used for the quality of service level of the will.
publish(topic, msg, retain=False, qos=0)
A message is sent from the client to the agent and then sent from the agent to any client that subscribes to the matching topic, parameters:
topic The topic of the message should be posted.
msg The actual message to send.
retain If set to True, the will message will be set to the last known
good/reserved message
for the topic.qos The level of quality of service to use.
subscribe(topic, qos=0)
Subscribe to a topic of the service, this module provides some helper functions to subscribe and process messages directly. For example set_callback
, parameters:
topic The subject of the message to subscribe.
qos The level of quality of service to use.
set_callback(f)
Sets the callback function for the topic subscription, which is called when the server responds to our subscription request, parameters:
f callback function.
wait_msg()
Wait for the server until the server has no pending messages. This function is a blocking function.
check_msg()
Check if the server has pending messages. If not, return directly, if any, do same processing as function wait_msg.
Sample Code:
Last updated