开发者

Am I using headers properly in python Twisted using getPage?

开发者 https://www.devze.com 2023-04-11 08:34 出处:网络
Below us my callback for fetching a page using twisted. client.getPage(iUrl,headers,method=\'GET\',cookies=cj).addCallback(self.processPage,iUrl).addErrback(self.printError,iUrl)

Below us my callback for fetching a page using twisted.

     client.getPage(iUrl,headers,method='GET',cookies=cj).addCallback(self.processPage,iUrl).addErrback(self.printError,iUrl)

Here is the format for my headers.

headers = Headers({'content-type': ['text/html; charset=utf-8'], 'user-agent': ["Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11'"]})

I know the user 开发者_C百科agent works if I use urllib2 because I can extract fields that require a header. Using this header in twisted does not work and I suspect how I am using headers in twisted. So, what is to proper way to specify a header in my code?

Thanks


The headers parameter to twisted.web.client.getPage accepts a dict, not a twisted.web.http_headers.Headers instance.


HTTPClientFactory (used internally by getPage) constructor signature looks like:

3       def __init__(self, url, method='GET', postdata=None, headers=None,
204                  agent="Twisted PageGetter", timeout=0, cookies=None,
205                  followRedirect=1):

thus try passing headers as keyword param:

client.getPage(iUrl,method='GET',cookies=cj, headers=headers)...

headers themselves look quite normal for me.

0

精彩评论

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