get-with-headers: don't block indefinitely if the server had an internal error
If the server had an internal error and returned 500, there's nothing
to read, so "response.read()" blocks indefinitely. Only output the
response if there's really a response.
[ original upstream message ]
#!/usr/bin/env python"""This does HTTP GET requests given a host:port and path and returnsa subset of the headers plus the body of the result."""importhttplib,systry:importmsvcrt,osmsvcrt.setmode(sys.stdout.fileno(),os.O_BINARY)msvcrt.setmode(sys.stderr.fileno(),os.O_BINARY)exceptImportError:passtwice=Falseif'--twice'insys.argv:sys.argv.remove('--twice')twice=Trueheaderonly=Falseif'--headeronly'insys.argv:sys.argv.remove('--headeronly')headeronly=Truereasons={'Not modified':'Not Modified'}# python 2.4tag=Nonedefrequest(host,path,show):assertnotpath.startswith('/'),pathglobaltagheaders={}iftag:headers['If-None-Match']=tagconn=httplib.HTTPConnection(host)conn.request("GET",'/'+path,None,headers)response=conn.getresponse()printresponse.status,reasons.get(response.reason,response.reason)ifshow[:1]==['-']:show=sorted(hforh,vinresponse.getheaders()ifh.lower()notinshow)forhin[h.lower()forhinshow]:ifresponse.getheader(h,None)isnotNone:print"%s: %s"%(h,response.getheader(h))ifnotheaderonly:printifresponse.status!=500:data=response.read()sys.stdout.write(data)iftwiceandresponse.getheader('ETag',None):tag=response.getheader('ETag')returnresponse.statusstatus=request(sys.argv[1],sys.argv[2],sys.argv[3:])iftwice:status=request(sys.argv[1],sys.argv[2],sys.argv[3:])if200<=status<=305:sys.exit(0)sys.exit(1)