使用python的coap协议获取方法

使用python的coap协议获取方法,python,protocols,iot,coap,Python,Protocols,Iot,Coap,我正在尝试使用coap运行应用程序,但我是新手。我正在使用python coapthon3库。但是我想使用编码路径从库中获取有效负载。但我不能这样做。我的客户代码如下。 多谢各位 from coapthon.client.helperclient import HelperClient host = "127.0.0.1" port = 5683 path = "encoding" payload = 'text/plain' client = HelperClient(server=(ho

我正在尝试使用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()
从coapthon.client.helperclient导入helperclient
host=“127.0.0.1”
端口=5683
path=“编码”
有效负载='text/plain'
客户端=帮助客户端(服务器=(主机、端口))
response=client.get(path+'application/xml'+'“+str(有效负载)+')
client.stop()

不,不应该将所有内容连接到路径

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

因此,您需要创建一个请求并填写所有需要的字段,并使用发送请求方法

我想我的片段不是那个蟒蛇,所以请耐心听我说

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()
从coapthon.client.helperclient导入helperclient
从coapthon.messages.request导入请求
从coapthon导入定义
host=“127.0.0.1”
端口=5683
path=“编码”
有效负载='text/plain'
客户端=帮助客户端(服务器=(主机、端口))
请求=请求()
request.code=defines.Codes.GET.number
request.type=defines.Types['NON']
request.destination=(主机、端口)
request.uri_path=path
request.content\u type=定义.content\u类型[“应用程序/xml”]
request.payload='“+str(payload)+'”
响应=客户端。发送请求(请求)
client.stop()

谢谢,但我对代码有问题。当我运行此代码时,内容类型变为int.服务器端资源。我无法将代码添加到注释中。很抱歉,谢谢,找到了解决办法。