Skip to content

Instantly share code, notes, and snippets.

@marcesher
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcesher/7ced727e0f643f4ea9f8 to your computer and use it in GitHub Desktop.
Save marcesher/7ced727e0f643f4ea9f8 to your computer and use it in GitHub Desktop.
Strange hanging behavior with python httplib in one environment only
# Problem: In only one environment, I see behavior with httplib where issuing a request against a FQDN will hang, but works fine with a URL without the domain
# eg request("GET", "http://google.com") hangs, but request("GET", "/") does not
# I only see this problem in one of our hosting environments, on both 2.6 and 2.7
# I cannot replicate it anywhere else
import httplib
cn = httplib.HTTPConnection("google.com", 80, timeout=5)
cn.set_debuglevel(10)
cn.request("GET", "/")
res = cn.getresponse()
print("\n\nResponse length: %s\n\n" % len(res.read()) )
# This hangs in our hosting environment, but I can't replicate it anywhere else
cn.request("GET", "http://google.com/")
res = cn.getresponse()
print("\n\nResponse length: %s\n\n" % len(res.read()) )
$ python2.7 test.py
send: 'GET / HTTP/1.1\r\nHost: google.com\r\nAccept-Encoding: identity\r\n\r\n'
reply: 'HTTP/1.1 301 Moved Permanently\r\n'
header: Location: http://www.google.com/
header: Content-Type: text/html; charset=UTF-8
header: Date: Wed, 19 Aug 2015 12:35:52 GMT
header: Expires: Fri, 18 Sep 2015 12:35:52 GMT
header: Cache-Control: public, max-age=2592000
header: Server: gws
header: Content-Length: 219
header: X-XSS-Protection: 1; mode=block
header: X-Frame-Options: SAMEORIGIN
Response length: 219
send: 'GET http://google.com/ HTTP/1.1\r\nHost: google.com\r\nAccept-Encoding: identity\r\n\r\n'
Traceback (most recent call last):
File "test.py", line 11, in <module>
res = cn.getresponse()
File "/usr/lib64/python2.7/httplib.py", line 1013, in getresponse
response.begin()
File "/usr/lib64/python2.7/httplib.py", line 402, in begin
version, status, reason = self._read_status()
File "/usr/lib64/python2.7/httplib.py", line 360, in _read_status
line = self.fp.readline()
File "/usr/lib64/python2.7/socket.py", line 430, in readline
data = recv(1)
socket.timeout: timed out
@willbarton
Copy link

I'd be curious to see you try this in a file:

GET http://google.com/ HTTP/1.1
Host: google.com
Accept-Encoding: identity

And then:

nc www.google.com 80 < my_http_get_file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment