简体   繁体   中英

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

I am getting errors when setting up a python server with sanic. The server starts fine - but invoking via Terminal curl localhost:8000 -i leads to an error. Basically I want to call asynchronously a python function. The function parses a BLE temperature and humidity sensor. The python function also works fine. I am using the python function since I don't have any equivalent in JavaScript.

I tried to change the return json values but none of the changes worked out.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM