本文整理汇总了Python中six.moves.http_cookiejar.CookieJar.extract_cookies方法的典型用法代码示例。如果您正苦于以下问题:Python CookieJar.extract_cookies方法的具体用法?Python CookieJar.extract_cookies怎么用?Python CookieJar.extract_cookies使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.http_cookiejar.CookieJar
的用法示例。
在下文中一共展示了CookieJar.extract_cookies方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extract_cookiejar
# 需要导入模块: from six.moves.http_cookiejar import CookieJar [as 别名]
# 或者: from six.moves.http_cookiejar.CookieJar import extract_cookies [as 别名]
def extract_cookiejar(self):
jar = CookieJar()
# self._respose could be None
# if this method is called from custom preapre response
# function called from spider cache backend
if self._response and self._request:
jar.extract_cookies(
# pylint: disable=protected-access
MockResponse(self._response._original_response.msg),
# pylint: enable=protected-access
MockRequest(self._request),
)
return jar
示例2: extract_cookiejar
# 需要导入模块: from six.moves.http_cookiejar import CookieJar [as 别名]
# 或者: from six.moves.http_cookiejar.CookieJar import extract_cookies [as 别名]
def extract_cookiejar(self, resp, req):
jar = CookieJar()
jar.extract_cookies(MockResponse(resp._original_response.msg),
MockRequest(req))
return jar
示例3: HttpTransport
# 需要导入模块: from six.moves.http_cookiejar import CookieJar [as 别名]
# 或者: from six.moves.http_cookiejar.CookieJar import extract_cookies [as 别名]
class HttpTransport(Transport):
"""
HTTP transport using urllib2. Provided basic http transport
that provides for cookies, proxies but no authentication.
"""
def __init__(self, **kwargs):
"""
@param kwargs: Keyword arguments.
- B{proxy} - An http proxy to be specified on requests.
The proxy is defined as {protocol:proxy,}
- type: I{dict}
- default: {}
- B{timeout} - Set the url open timeout (seconds).
- type: I{float}
- default: 90
"""
Transport.__init__(self)
Unskin(self.options).update(kwargs)
self.cookiejar = CookieJar()
self.proxy = {}
self.urlopener = None
def open(self, request):
try:
url = request.url
log.debug('opening (%s)', url)
u2request = urllib.request.Request(url)
self.proxy = self.options.proxy
return self.u2open(u2request)
except urllib.error.HTTPError as e:
raise TransportError(str(e), e.code, e.fp)
def send(self, request):
result = None
url = request.url
msg = request.message
headers = request.headers
try:
u2request = urllib.request.Request(url, msg, headers)
self.addcookies(u2request)
self.proxy = self.options.proxy
request.headers.update(u2request.headers)
log.debug('sending:\n%s', request)
fp = self.u2open(u2request)
self.getcookies(fp, u2request)
result = Reply(200, fp.headers.__dict__, fp.read())
log.debug('received:\n%s', result)
except urllib.error.HTTPError as e:
if e.code in (202, 204):
result = None
else:
raise TransportError(e.msg, e.code, e.fp)
return result
def addcookies(self, u2request):
"""
Add cookies in the cookiejar to the request.
@param u2request: A urllib2 request.
@rtype: u2request: urllib2.Requet.
"""
self.cookiejar.add_cookie_header(u2request)
def getcookies(self, fp, u2request):
"""
Add cookies in the request to the cookiejar.
@param u2request: A urllib2 request.
@rtype: u2request: urllib2.Requet.
"""
self.cookiejar.extract_cookies(fp, u2request)
def u2open(self, u2request):
"""
Open a connection.
@param u2request: A urllib2 request.
@type u2request: urllib2.Requet.
@return: The opened file-like urllib2 object.
@rtype: fp
"""
tm = self.options.timeout
url = self.u2opener()
return url.open(u2request, timeout=tm)
def u2opener(self):
"""
Create a urllib opener.
@return: An opener.
@rtype: I{OpenerDirector}
"""
if self.urlopener is None:
return urllib.request.build_opener(*self.u2handlers())
else:
return self.urlopener
def u2handlers(self):
"""
Get a collection of urllib handlers.
@return: A list of handlers to be installed in the opener.
@rtype: [Handler,...]
"""
#.........这里部分代码省略.........