urequests - Timeout

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Posts: 9
Joined: Tue Oct 03, 2017 12:09 pm

urequests - Timeout

Post by ad525 » Mon Mar 05, 2018 8:42 pm

I try to make post requests using urequests lib to an API. When the API is not responding the request is stuck in a "loop" and after a while I receive : ssl_handshake_status: -256

Even if the API is getting alive I could not make any request if I received ssl_handshake error.

How could I implement a timeout for urequests ?


Posts: 9
Joined: Tue Oct 03, 2017 12:09 pm

Re: urequests - Timeout

Post by ad525 » Wed Mar 21, 2018 11:37 am

I made a pull request : https://github.com/micropython/micropython-lib/pull/263. The code was tested by me on an esp8266 board and seems to be ok. Could someone test it on other ports ?

Posts: 2
Joined: Sun Nov 18, 2018 10:53 pm

Re: urequests - Timeout

Post by KJM » Sun Jun 23, 2019 1:48 am

Doesn't work on Pycom's version of urequests. File "/flash/lib/urequests.py", line 112, in get
TypeError: function does not take keyword arguments

import usocket

class Response:

def __init__(self, f):
self.raw = f
self.encoding = "utf-8"
self._cached = None

def close(self):
if self.raw:
self.raw = None
self._cached = None

def content(self):
if self._cached is None:
self._cached = self.raw.read()
self.raw = None
return self._cached

def text(self):
return str(self.content, self.encoding)

def json(self):
import ujson
return ujson.loads(self.content)

def request(method, url, data=None, json=None, headers={}, stream=None):
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
raise ValueError("Unsupported protocol: " + proto)

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

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

s = usocket.socket(ai[0], ai[1], ai[2])

if timeout is not None:
assert hasattr(usocket.socket, 'settimeout'), 'Socket does not support timeout'

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(b": ")
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))
if data:

l = s.readline()
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":
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:

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

def head(url, **kw):
return request("HEAD", url, **kw)

def get(url, **kw):
return request("GET", url, **kw)

def post(url, **kw):
return request("POST", url, **kw)

def put(url, **kw):
return request("PUT", url, **kw)

def patch(url, **kw):
return request("PATCH", url, **kw)

def delete(url, **kw):
return request("DELETE", url, **kw)

Post Reply