Path: csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'finally:': 0.07; 'socket': 0.07; 'sys': 0.07; 'urllib2': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; '"python': 0.16; 'gai': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'seconds.': 0.16; 'subject:issue': 0.16; 'time.time()': 0.16; 'url:example': 0.16; 'all.': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'server,': 0.19; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'script': 0.25; 'suggested': 0.26; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; "i'm": 0.30; 'run': 0.32; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'version': 0.36; "didn't": 0.36; 'similar': 0.36; 'skip:- 20': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'below:': 0.68; 'results': 0.69; 'difference.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: urllib2 timeout issue Date: Wed, 16 Oct 2013 13:22:34 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084a72e.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381922512 news.xs4all.nl 15874 [2001:888:2000:d::a6]:57171 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56878 Jérôme wrote: > Hi all. > > I'm having troubles with urllib2 timeout. > > See the following script : > > ---------------------------- > import urllib2 > result = urllib2.urlopen("http://dumdgdfgdgmyurl.com/") > print result.readline() > ---------------------------- > > If run on my Debian Wheezy computer, or on my Debian Squeeze server, > the answer is instantaneous : > > [...] > urllib2.URLError: > > When run on my Raspberry Pi with Raspian Wheezy, the answer is > identical but it takes 10 seconds. > > I tried > > result = urllib2.urlopen("http://dumdgdfgdgmyurl.com/", timeout=5) > > but I get the same results : instantaneous on Debian, 10 secondes on > RPi. > > I also added this, as suggested on some StackOverflow pages : > > import socket > socket.setdefaulttimeout(5) > > and it didn't make any difference. > > In both cases, Python version is "Python 2.7.3". > > Am I missing something ? The problem might be ipv6-related. I'm currently working around a similar annoying delay with the monkey-patch demonstrated below: $ cat patch_getaddrinfo.py import sys import time import urllib2 def monkeypatch_getaddrinfo(): import socket gai = socket.getaddrinfo def getaddrinfo(*args): return gai(args[0], args[1], 2, *args[3:]) socket.getaddrinfo = getaddrinfo if "-4" in sys.argv: monkeypatch_getaddrinfo() start = time.time() try: urllib2.urlopen("http://example.com/") finally: print time.time() - start $ python -S patch_getaddrinfo.py 20.320786953 $ python -S patch_getaddrinfo.py -4 0.305027008057 As you may infer from the above I don't know what is happening exactly and how to fix it properly...