1

I am experiencing strange behavior with urllib2.urlopen() on Ubuntu 10.10. The first request to a url goes fast but the second takes a long time to connect. I think between 5 and 10 seconds. On windows this just works normal?

Does anybody have an idea what could cause this issue?

Thanks, Onno

jsbueno
  • 99,910
  • 10
  • 151
  • 209
Fino
  • 361
  • 1
  • 2
  • 11

2 Answers2

3

5 seconds sounds suspiciously like the DNS resolving timeout.

A hunch, It's possible that it's cycling through the DNS servers in your /etc/resolv.conf and if one of them is broken, the default timeout is 5 seconds on linux, after which it will try the next one, looping back to the top when it's tried them all.

If you have multiple DNS servers listed in resolv.conf, try removing all but one. If this fixes it; then after that see why you're being assigned incorrect resolving servers.

Crast
  • 15,996
  • 5
  • 45
  • 53
  • You made my day! /etc/resolv.conf was pointing to my router. I changed this for a Google DNS server and both calls are up to normal speed. – Fino Nov 06 '10 at 07:40
1

you can enable debugging of the urllib2 maybe it can help you found out the problem

import urllib2

opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1))
opener.open('http://www.google.com')
mouad
  • 67,571
  • 18
  • 114
  • 106
  • Thanks, I already used debuglevel=1 but it doesn't give me any leads. Both debugging info is the same. – Fino Nov 06 '10 at 07:30