【问题标题】:coap protocol get method use pythoncoap协议get方法使用python
【发布时间】:2018-11-05 14:07:12
【问题描述】:

我正在尝试使用 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()

【问题讨论】:

    标签: python protocols iot coap


    【解决方案1】:

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

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

    所以你需要创建一个请求并填写所有需要的字段,并使用 send_request 方法。

    我猜我的 sn-p 不是 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()
    

    【讨论】:

    • 谢谢,但我的代码有问题。当我运行此代码时, content_type 变为 int。服务器端资源。我无法在评论中添加代码。我很抱歉
    • 谢谢,找到了解决办法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多