首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从micropython运行urequests.post的情况下获取OSError:ENOMEM?

从micropython运行urequests.post的情况下获取OSError:ENOMEM?

提问于 2022-11-30 22:49:15
回答 0关注 0查看 287

在我调用urequests.requests()的时候,出现问题, 根据error库文件中内容,可以得知错误的内容为 Out of memory,大佬们,有什么解决的办法吗?

相关代码

代码语言:js
复制
url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=24.b9b3ce8c6d86e36bd075f9ddfc2a0105.2592000.1672198963.282335-28622156"
f = open("test.jpeg", "rb")
payload = binascii.b2a_base64(f.read())
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Accept': 'application/json'
}

response = urequests.request("POST", url, data=payload, headers=headers)
print(response.text)

报错信息

代码语言:js
复制
Traceback (most recent call last):
  File "<stdin>", line 39, in <module>
  File "/lib/urequests.py", line 60, in request
OSError: [Errno 12] ENOMEM
代码语言:js
复制
#库文件,提示的错误定位在下面我注释的那行
    def request(method, url, data=None, json=None, headers={}, stream=None):
    try:
        proto, dummy, host, path = url.split("/", 3)
    except ValueError:
        proto, dummy, host = url.split("/", 2)
        path = ""
    if proto == "http:":
        port = 80
    elif proto == "https:":
        import ussl
        port = 443
    else:
        raise ValueError("Unsupported protocol: " + proto)

    if ":" in host:
        host, port = host.split(":", 1)
        port = int(port)

    ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
    ai = ai[0]

    s = usocket.socket(ai[0], ai[1], ai[2])
    try:
        s.connect(ai[-1])
        if proto == "https:":
            s = ussl.wrap_socket(s, server_hostname=host) #报错的代码
        s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
        if not "Host" in headers:
            s.write(b"Host: %s\r\n" % host)
        # Iterate over keys to avoid tuple alloc
        for k in headers:
            s.write(k)
            s.write(b": ")
            s.write(headers[k])
            s.write(b"\r\n")
        if json is not None:
            assert data is None
            import ujson
            data = ujson.dumps(json)
            s.write(b"Content-Type: application/json\r\n")
        if data:
            s.write(b"Content-Length: %d\r\n" % len(data))
        s.write(b"\r\n")
        if data:
            s.write(data)

        l = s.readline()
        #print(l)
        l = l.split(None, 2)
        status = int(l[1])
        reason = ""
        if len(l) > 2:
            reason = l[2].rstrip()
        while True:
            l = s.readline()
            if not l or l == b"\r\n":
                break
            #print(l)
            if l.startswith(b"Transfer-Encoding:"):
                if b"chunked" in l:
                    raise ValueError("Unsupported " + l)
            elif l.startswith(b"Location:") and not 200 <= status <= 299:
                raise NotImplementedError("Redirects not yet supported")
    except OSError:
        s.close()
        raise

    resp = Response(s)
    resp.status_code = status
    resp.reason = reason
    return resp

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档