Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I set a timeout for urllib3 request?

How can I get urllib3 to try to reach a site for 10 seconds and return an error or data if it can’t?

http = urllib3.PoolManager()

page = http.request('get', link)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Based on Timeout configuration [urllib3-docs]
you can set a timeout for your request when you’re using urllib3 in all the below ways:

Timeouts can be defined as a default for a pool:

timeout = Timeout(connect=2.0, read=7.0)
http = PoolManager(timeout=timeout)
response = http.request('GET', 'http://example.com/')

Or per-request (which overrides the default for the pool):

response = http.request('GET', 'http://example.com/', >timeout=Timeout(10))

Timeouts can be disabled by setting all the parameters to None:

no_timeout = Timeout(connect=None, read=None)
response = http.request('GET', 'http://example.com/, >timeout=no_timeout)

And I think your solution is sth like below the code snippet:

from urllib3 import Timeout, Poolmanager

timeout = Timeout(connect=10.0, read=None)
http = PoolManager(timeout=timeout)
response = http.request('GET', 'http://example.com/)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading