簡體   English   中英

sanic( ) - AttributeError: 'Request' object 沒有屬性 'split'

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

使用 sanic 設置 python 服務器時出現錯誤。 服務器啟動正常 - 但通過終端curl localhost:8000 -i調用會導致錯誤。 基本上我想異步調用 python function。function 解析 BLE 溫度和濕度傳感器。 python function 也可以正常工作。 我正在使用 python function,因為我在 JavaScript 中沒有任何等效項。

我嘗試更改返回值 json,但沒有任何更改成功。

代碼

#!/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')

錯誤

[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'

關於如何解決它的任何建議?

所以我通過將backend = BluepyBackendpoller = MiTempBtPoller('sensormacID', backend)放在async def poll(mac)之前並刪除while True: ... sleep(10)循環來使代碼工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM