python-hyper / hyper

HTTP/2 for Python.
http://hyper.rtfd.org/en/latest/
MIT License
1.05k stars 191 forks source link

Add connection/read timeout for requests adapter #342

Closed hyxbiao closed 7 years ago

hyxbiao commented 7 years ago

[+] Add connection/read timeout for requests adapter, we can use like this:

import requests
from hyper.contrib import HTTP20Adapter
s = requests.Session()
s.mount('https://http2bin.org', HTTP20Adapter())
r = s.get('https://http2bin.org/get', timeout=30)
print(r.status_code)
hyxbiao commented 7 years ago

timeout is optional, the default value is None, means that it will block connection/read, it is the same as request/adapters.py: def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):

if isinstance(timeout, tuple):
    try:
        connect, read = timeout
        timeout = TimeoutSauce(connect=connect, read=read)
    except ValueError as e:
        # this may raise a string formatting error.
        err = ("Invalid timeout {0}. Pass a (connect, read) "
               "timeout tuple, or a single float to set "
               "both timeouts to the same value".format(timeout))
        raise ValueError(err)
else:
    timeout = TimeoutSauce(connect=timeout, read=timeout)

it also can set timeout to socket._GLOBAL_DEFAULT_TIMEOUT

hyxbiao commented 7 years ago

update information: add timeout for _create_tunnel funciton, almost forget it move timeout to _h1/2_kwargs in hyper/common/connection.py add more timeout tests