开发者

Why are my requests from urllib2 delayed with Apache2?

I'm dealing with two internal web servers, one running Apache2 on Ubuntu Server 10.04, and the other IIS on Windows Server 2008. When I hit either of the root URLs from a web browser with a cleared cache, calling the server by IP address, both pop up with no delay. I also see no delays browsing sites on either server. However, when I call the same addresses using urllib2 in Python, each request to the Apache2 server is delayed by 4.5 to 5 seconds. The IIS server responds in less than 0.02.

Here is a script I ran to verify the problem. I set the User-Agent in case it makes a difference, but it doesn't appear to:

import urllib2
from time import time

apache_server = 'http://192.168.1.101/'
iis_server = 'http://192.168.1.102/'

headers = {'User-Agent' : "Mozilla/5.0 (X11; U; Linux i586; de; rv:5.0) Gecko/20100101 Firefox/5.0",}

print('Contacting Apache2 server...')
request = urllib2.Request(url=apache_server, headers=headers)
start = time()
response = urllib2.urlopen(request).read()
elapsed = time() - start
print('Elapsed time: {0}'.format(elapsed))

print('Contacting IIS server...')
request = urllib2.Request(url=iis_server, headers=headers)
start = time()
response = urllib2.urlopen(request).read()
elapsed = time() - start
print('Elapsed time: {0}'.format(elapsed))

Results:

>python urltest.py
Contacting Apache2 server...
Elapsed time: 4.55500006676

Contacting IIS server...
Elapsed time: 0.0159997940063

What difference is there between a browser request and my urllib2 request that would explain this delay?

I've seen similar problems with SSH caused by reverse DNS lookup, but I have HostnameLookups Off in my Apache2 confi开发者_JS百科g. I don't know if anything else could be triggering a lookup.


Update: I followed the exchange with Wireshark, and found three failed NBNS queries going from my machine to the Apache2 server before they finally start talking. This accounted for the lost time. I tried adding an entry in hosts for the web server, which eliminated the delay.

I'm not adding this as an answer because I still don't know why the lookup attempt is happening, or why I don't see the same behavior from the web browser. I just have a workaround for whatever mistake I'm making.


The lookup is happening because that's what Windows does. It's always going to check NetBIOS before anything else.

Further reading:

  • Microsoft TCP/IP Host Name Resolution Order
  • NetBIOS over TCP/IP Name Resolution and WINS
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜