开发者

Python, Twisted and adding a cookie to getPage using cookielib

开发者 https://www.devze.com 2023-04-10 06:23 出处:网络
Below is my code for adding a cookie to get a page using getPage.Also is the error message I get.How to I add a cookie?Is cookie is None then the code works.

Below is my code for adding a cookie to get a page using getPage. Also is the error message I get. How to I add a cookie? Is cookie is None then the code works.

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




Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 59, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 37, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/selectreactor.py", line 146, in        _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 664, in doConnect
self._connectDone()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 671, in _connectDone
self.protocol.makeConnection(self)
File "/usr/lib/python2.7/dist-packages/twisted/internet/protocol.py", line 459, in makeConnection
self.connectionMade()
File "/usr/lib/python2.7/dist-packages/twisted/web/client.py", line 61, in connectionMade
   for cookie, cookval in self.factory.cookies.items():
exceptions.AttributeError: CookieJar instance has no attribute 'items'
*--- Failure #133 ---
Failure: exceptions.At开发者_开发百科tributeError: CookieJar instance has no attribute 'items'
*--- End of Failure #133 -


Looks like you're passing a cookiejar in the cookie argument. I can't find a reference for that working.

I've tried passing a simple dict and it works. Here's the full code:

from twisted.web import client
import pprint

def processPage (page, url):
    print len(page), 'bytes received'
    print 'cookies: ', pprint.pformat(cj)

def printError(error, url):
    print 'ERROR: ', error.getErrorMessage()

def end(ignore):
    reactor.stop()

iUrl = 'http://twistedmatrix.com'

headers = []

cj = {}
client.getPage(iUrl,headers,method='GET',cookies=cj).addCallback(processPage,iUrl).addErrback(printError,iUrl).addBoth(end)


from twisted.internet import reactor
reactor.run()

And here's output:

10660 bytes received
cookies: 
{'trac_form_token': '940bfe8055b63872303017ba',
 'trac_session': '5a3500ec1bc8efad40974a31'}
0

精彩评论

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

关注公众号