简体   繁体   English

sanic( ) - AttributeError: 'Request' object 没有属性 'split'

[英]sanic( ) - AttributeError: 'Request' object has no attribute 'split'

I am getting errors when setting up a python server with sanic.使用 sanic 设置 python 服务器时出现错误。 The server starts fine - but invoking via Terminal curl localhost:8000 -i leads to an error.服务器启动正常 - 但通过终端curl localhost:8000 -i调用会导致错误。 Basically I want to call asynchronously a python function. The function parses a BLE temperature and humidity sensor.基本上我想异步调用 python function。function 解析 BLE 温度和湿度传感器。 The python function also works fine. python function 也可以正常工作。 I am using the python function since I don't have any equivalent in JavaScript.我正在使用 python function,因为我在 JavaScript 中没有任何等效项。

I tried to change the return json values but none of the changes worked out.我尝试更改返回值 json,但没有任何更改成功。

Code :代码

#!/usr/bin/env python3

from sanic import Sanic
from sanic.response import json

now = datetime.now()

dt_string = now.strftime("%d/%m %H:%M:%S")

app = Sanic()
@app.route('/')
async def poll(mac):
    """Poll data from the sensor every x (60) seconds."""
    while True:
        backend = BluepyBackend
        poller = MiTempBtPoller(mac, backend)
        print("Parsing data")
        print("FW: {}".format(poller.firmware_version()))
        print("Name: {}".format(poller.name()))
        print("Battery: {}".format(poller.parameter_value(MI_BATTERY)))
        print("Temperature: {}".format(poller.parameter_value(MI_TEMPERATURE)))
        print("Humidity: {}".format(poller.parameter_value(MI_HUMIDITY)))
        with open('/home/pi/file.txt', 'a') as myfile:
            myfile.write("Time: {}, Battery: {}; Temp: {}; Humidity: {}".format(datetime.now(), poller.parameter_value(MI_BATTERY), poller.parameter_value(MI_TEMPERATURE), \ 
            poller.parameter_value(MI_HUMIDITY)))
        return json.dumps({"a": poller.parameter_value(MI_TEMPERATURE)})
        sleep(60)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port = 8000)
    poll('sensormac')

Error :错误

[2020-11-15 17:46:39 +0100] [1557] [INFO] Goin' Fast @ http://0.0.0.0:8000
[2020-11-15 17:46:39 +0100] [1557] [INFO] Starting worker [1557]
Parsing data
[2020-11-15 17:46:43 +0100] [1557] [ERROR] Exception occurred while handling uri: 'http://localhost:8000/'
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/sanic/app.py", line 939, in handle_request
    response = await response
  File "/home/pi/mitemp/btparser_server.py", line 25, in poll
    print("FW: {}".format(poller.firmware_version()))
  File "/home/pi/mitemp/mitemp_bt/mitemp_bt_poller.py", line 90, in firmware_version
    with self._bt_interface.connect(self._mac) as connection:
  File "/home/pi/mitemp/btlewrap/base.py", line 47, in __enter__
    self._backend.connect(self._mac)
  File "/home/pi/mitemp/btlewrap/bluepy.py", line 27, in _func_wrapper
    return func(*args, **kwargs)
  File "/home/pi/mitemp/btlewrap/bluepy.py", line 56, in connect
    self._peripheral = Peripheral(mac, iface=iface, addrType=self.address_type)
  File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 391, in __init__
    self._connect(deviceAddr, addrType, iface)
  File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 422, in _connect
    if len(addr.split(":")) != 6:
AttributeError: 'Request' object has no attribute 'split'

Any advice on how to fix it?关于如何解决它的任何建议?

So I got the code working by putting the backend = BluepyBackend and poller = MiTempBtPoller('sensormacID', backend) before the async def poll(mac) AND removing the while True: ... sleep(10) loop所以我通过将backend = BluepyBackendpoller = MiTempBtPoller('sensormacID', backend)放在async def poll(mac)之前并删除while True: ... sleep(10)循环来使代码工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError问题:'WebDriver'对象没有属性'manage' - Issue with AttributeError: 'WebDriver' object has no attribute 'manage' AttributeError:'WebElement'对象没有属性'getElementById' - AttributeError: 'WebElement' object has no attribute 'getElementById' AttributeError:'str'对象没有属性'resolve' - AttributeError: 'str' object has no attribute 'resolve' AttributeError:'NoneType'对象没有属性'is_active' - AttributeError: 'NoneType' object has no attribute 'is_active' AttributeError: 'tuple' object 在 Postman 中使用 POST 请求时没有属性 'get' - AttributeError: 'tuple' object has no attribute 'get' while using POST request in Postman 安装 CORS 后 flask 服务器出错:AttributeError: 'FlaskApp' object 没有属性 'after_request' - Error with flask server after installing CORS: AttributeError: 'FlaskApp' object has no attribute 'after_request' / accounts / regist_save /'User'对象上的AttributeError没有属性'user' - AttributeError at /accounts/regist_save/ 'User' object has no attribute 'user' AttributeError: 'list' 对象没有使用 Selenium 和 Python 的属性 'click' - AttributeError: 'list' object has no attribute 'click' using Selenium and Python AttributeError: 'list' object 没有属性 'filtered' (Odoo 14) - AttributeError: 'list' object has no attribute 'filtered' (Odoo 14) Django: AttributeError at /update_item/ 'WSGIRequest' object 没有属性 'data' - Django: AttributeError at /update_item/ 'WSGIRequest' object has no attribute 'data'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM