【问题标题】:getting OSError -202 where running urequests.get from micropy从 micropy 运行 urequests.get 时获取 OSError -202
【发布时间】:2020-09-28 08:17:15
【问题描述】:

你好,这段代码有错误,但它在 python shell 中运行,任何人都可以帮助我

from machine import Pin
import time
import network
import urequests
p0 = Pin(0,Pin.OUT)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('ssid', 'pass')
response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
while True:
    ans = response.json()['userId']
    p0.value(1)
    time.sleep(1)
    p0.off()
    time.sleep(1)
    print('ok')

这是错误:

Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
  File "urequests.py", line 108, in get
  File "urequests.py", line 53, in request
OSError: -202

【问题讨论】:

  • 我在网上找到的关于 OSError 的唯一内容是与 SSL 相关的内容,只是为了确保您可以在 get 请求中将 http 更改为 https 吗?
  • -202 seems 与失败的getaddrinfo() 调用有关。

标签: python esp32 micropython


【解决方案1】:

您的问题(我的猜测)是您开始 urequest.get() 没有连接到 WiFi。创建进行 wifi 连接的函数并调用它

def do_connect():
    import network
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('essid', 'password')
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())

说明:wlan.connect()是异步函数,你需要等待,它连接到wifi后才继续urequest.get()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多