MicroPython urequests.post失败 [英] MicroPython urequests.post failures

查看:3047
本文介绍了MicroPython urequests.post失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过运行MicroPython的ESP8266 MCU在我的(Android)移动设备上接收通知。出于这个原因,我订阅了几个在线服务,为此任务公开了一些API,Pushbullet和Pushed,我在我的设备上安装了匹配的应用程序。

I'm trying to receive notifications on my (Android) mobile device from an ESP8266 MCU running MicroPython. For this reason I subscribed to a couple of online services exposing some APIs for this task, Pushbullet, and Pushed, and I installed the matching apps on my device.

这是我正在尝试的事情:

This is what I'm trying:

Pushbullet:

Pushbullet:

import json
import urequests

body = "Test Notification"
title = "Pushbullet"
data_sent = {"type": "note", "title": title, "body": body}
API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'

pb_headers = {
    'Authorization': 'Bearer ' + API_KEY, 
    'Content-Type': 'application/json'
}

r = urequests.post(
    'https://api.pushbullet.com/v2/pushes', 
    data=json.dumps(data_sent), 
    headers=pb_headers
)

print(r)

错误:

ssl_handshake_status: -256
Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
  File "urequests.py", line 104, in post
  File "urequests.py", line 56, in request
OSError: [Errno 5] EIO

推送:

import json
import urequests

payload = {
  "app_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "app_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "target_type": "app",
  "content": "Remote Mic MCU test from ESP8266"
}

r = urequests.post("https://api.pushed.co/1/push", data=payload)
print(r)

错误:

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "urequests.py", line 104, in post
  File "urequests.py", line 74, in request
TypeError: object with buffer protocol required

搜索这些错误,不会让我感到有用。

Searching for these errors, doesn't get me anywhere useful.

完全相同的代码片段在我的Linux机器上正常工作(使用 requests 而不是 urequests ),但我理解 urequests 可能有一些限制。

The exact same code snippets work OK on my Linux box (using requests instead of urequests), but I understand that urequests may have some limitations.

您是否有任何关于如何解决这个问题的提示?

Do you have any hint on how to fix this?

推荐答案

异常消息表明您传递了 urequests 不期望的数据类型。根据我对HTTP POST工作原理的了解(参见HTTP标准),我知道它接受八位字节流,在Python中它将由 str 表示bytes 类型。而你通过字典。
`

The exception message suggests that you pass the type of data which urequests doesn't expect. From my knowledge of how HTTP POST works (see HTTP standard), I know that it accepts octet stream, which in Python would be represented by str or bytes type. Whereas you pass a dictionary. `

这篇关于MicroPython urequests.post失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆