Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests GET takes a long time to respond to some requests

I am using Python requests get method to query the MediaWiki API, but it takes a lot of time to receive the response. The same requests receive the response very fast through a web browser. I have the same issue requesting google.com. Here are the sample codes that I am trying in Python 3.5 on Windows 10:

response = requests.get("https://www.google.com")
response = requests.get("https://en.wikipedia.org/wiki/Main_Page")
response = requests.get("http://en.wikipedia.org/w/api.php?", params={'action':'query', 'format':'json', 'titles':'Labor_mobility'})

However, I don't face this issue retrieving other websites like:

response = requests.get("http://www.stackoverflow.com")
response = requests.get("https://www.python.org/")
like image 368
1man Avatar asked Jul 06 '16 01:07

1man


People also ask

How do I send faster request in Python?

Solution #1: The Synchronous Way We can leverage the Session object to further increase the speed. The Session object will use urllib3's connection pooling, which means, for repeating requests to the same host, the Session object's underlying TCP connection will be re-used, hence gain a performance increase.

Are Python requests slow?

Python requests is slow and takes very long to complete HTTP or HTTPS request - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

How can I speed up API requests?

Caching is one of the best ways to improve API performance. If you have requests that frequently produce the same response, a cached version of the response avoids excessive database queries. The easiest way to cache responses is to periodically expire it, or force it to expire when certain data updates happen.

What does requests get () do?

The get() method sends a GET request to the specified url.


1 Answers

This sounds like there is an issue with the underlying connection to the server, because requests to other URLs work. These come to mind:

  • The server might only allow specific user-agent strings

Try adding innocuous headers, e.g.: requests.get("https://www.example.com", headers={"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"})

  • The server rate-limits you

Wait for a few minutes, then try again. If this solves your issue, you could slow down your code by adding time.sleep() to prevent being rate-limited again.

  • IPv6 does not work, but IPv4 does

Verify by executing curl --ipv6 -v https://www.example.com. Then, compare to curl --ipv4 -v https://www.example.com. If the latter is significantly faster, you might have a problem with your IPv6 connection. Check here for possible solutions.

Didn't solve your issue?

If that did not solve your issue, I have collected some other possible solutions here.

like image 181
vauhochzett Avatar answered Oct 23 '22 03:10

vauhochzett