0

This request never returns (or at least not within my patience):

import requests r = requests.get('http://en.wikipedia.org/w/api.php?rcprop=ids&format=json&action=query&rclimit=10&rctype=edit&list=recentchanges&rcnamespace=0', headers={'user-agent': 'api test'}) 

Hitting Ctrl+C always produces this traceback:

^CTraceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get return request('get', url, **kwargs) File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request return session.request(method=method, url=url, **kwargs) File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 383, in request resp = self.send(prep, **send_kwargs) File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 486, in send r = adapter.send(request, **kwargs) File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 330, in send timeout=timeout File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 542, in urlopen body=body, headers=headers) File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 367, in _make_request conn.request(method, url, **httplib_request_kw) File "/usr/lib/python2.7/httplib.py", line 973, in request self._send_request(method, url, body, headers) File "/usr/lib/python2.7/httplib.py", line 1007, in _send_request self.endheaders(body) File "/usr/lib/python2.7/httplib.py", line 969, in endheaders self._send_output(message_body) File "/usr/lib/python2.7/httplib.py", line 829, in _send_output self.send(msg) File "/usr/lib/python2.7/httplib.py", line 791, in send self.connect() File "/usr/lib/python2.7/httplib.py", line 772, in connect self.timeout, self.source_address) File "/usr/lib/python2.7/socket.py", line 562, in create_connection sock.connect(sa) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) 

Adding timeout=5 to the request causes the request to succeed, after the timeout has expired (ie the correct data is returned from the API request). But of course that adds five seconds of latency into my application for every API request.

What's going wrong here?

2
  • Do you use python3? I use python2.7.5 OK! Commented Apr 10, 2015 at 2:00
  • your request worked fine when I tried it, both python 3.4 and 2.7, could be related to something else Commented Apr 10, 2015 at 2:07

1 Answer 1

1

This was due to IPv6 not working very well on my network. httplib (and therefore Requests) seems to prefer IPv6 if it's available, but if it's not working very well then you can have a long wait while the IPv6 request times out. Setting a timeout causes it to fall back to IPv4 following the expiry of the timeout, which then succeeds. Disabling IPv6 on my network has fixed this (as, I assume, would fixing IPv6).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.