coap协议get方法使用python

2024-06-28 16:07:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用coap运行一个应用程序,但我是新来的。我正在使用python coapthon3库。但我想用编码路径从库中获取有效载荷。但我做不到。我的客户端代码如下。 谢谢你

from coapthon.client.helperclient import HelperClient

host = "127.0.0.1"
port = 5683
path = "encoding"
payload = 'text/plain'

client = HelperClient(server=(host, port))
response = client.get(path + 'application/xml' + '<value>"+str(payload)+"</value>')
client.stop()

Tags: path代码路径client应用程序host客户端编码
1条回答
网友
1楼 · 发布于 2024-06-28 16:07:42

不,你不应该把所有的东西都连接到路径上。在

不幸的是HelperClient#get没有提供指定有效负载的功能,尽管根据CoAP规范这是非常合法的

因此,您需要创建一个请求并填充所有需要的字段,并使用send_request方法。在

我想我的片段不是Python,所以请你容忍我。在

from coapthon.client.helperclient import HelperClient
from coapthon.messages.request import Request
from coapthon import defines

host = "127.0.0.1"
port = 5683
path = "encoding"
payload = 'text/plain'

client = HelperClient(server=(host, port))

request = Request()
request.code = defines.Codes.GET.number
request.type = defines.Types['NON']
request.destination = (host, port)
request.uri_path = path
request.content_type = defines.Content_types["application/xml"]
request.payload = '<value>"+str(payload)+"</value>'
response = client.send_request(request)

client.stop()

相关问题 更多 >