Search code examples
pythontwisted

Making Head Requests in Twisted


I am relatively new to using Twisted and I am having trouble returning the content-length header when performing a basic head request. I have set up an asynchronous client already but the trouble comes in this bit of code:

def getHeaders(url):
    d = Agent(reactor).request("HEAD", url)
    d.addCallbacks(handleResponse, handleError)
    return d

def handleResponse(r):
    print r.code, r.headers

    whenFinished = twisted.internet.defer.Deffered()
    r.deliverBody(PrinterClient(whenFinished))

    return whenFinished

I am making a head request and passing the url. As indicated in this documentation the content-length header is not stored in self.length, but can be accessed from the self.headers response. The output is returning the status code as expected but the header output is not what is expected. Using "uhttp://www.espn.go.com" as an example it currently returns:

Set-Cookie: SWID=77638195-7A94-4DD0-92A5-348603068D58; 
path=/; expires=Fri, 31-Jan-2034 00:50:09 GMT; domain=go.com;
X-Ua-Compatible: IE=edge,chrome=1
Cache-Control: max-age=15
Date: Fri, 31 Jan 2014 00:50:09 GMT
P3P: CP="CAO DSP COR CURa ADMa DEVa TAIa PSAa PSDa IVAi IVDi CONi 
OUR SAMo OTRo BUS PHY  ONL UNI PUR COM NAV INT DEM CNT STA PRE"
Content-Type: text/html; charset=iso-8859-1

As you can see, no content-length field is returned. If the same request is done in requests then the result will contain the content-length header:

r = requests.head("http://www.espn.go.com")
r.headers
({'content-length': '46133', 'content-encoding': 'gzip'...})
(rest omitted for readability)

What is causing this problem? I am sure it is a simple mistake on my part but I for the life of me cannot figure out what I have done wrong. Any help is appreciated.


Solution

  • http://www.espn.go.com/ returns one response if the client sends an Accept-Encoding: gzip header and another response if it doesn't.

    One of the differences between the two responses is the inclusion of the Content-Length header.

    If you want to make requests using Agent including Accept-Encoding: gzip then take a look at ContentDecoderAgent or the third-party treq package.