def domain_return_ok(self, domain, request):
"""Return false if cookies should not be returned, given cookie domain.
This is here as an optimization, to remove the need for checking every
cookie with a particular domain (which may involve reading many files).
The default implementations of domain_return_ok and path_return_ok
(return True) leave all the work to return_ok.
If domain_return_ok returns true for the cookie domain, path_return_ok
is called for the cookie path. Otherwise, path_return_ok and return_ok
are never called for that cookie domain. If path_return_ok returns
true, return_ok is called with the Cookie object itself for a full
check. Otherwise, return_ok is never called for that cookie path.
Note that domain_return_ok is called for every *cookie* domain, not
just for the *request* domain. For example, the function might be
called with both ".acme.com" and "www.acme.com" if the request domain
is "www.acme.com". The same goes for path_return_ok.
For argument documentation, see the docstring for return_ok.
"""
return True
python类Cookie()的实例源码
_clientcookie.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 16
收藏 0
点赞 0
评论 0
def domain_return_ok(self, domain, request):
"""Return false if cookies should not be returned, given cookie domain.
This is here as an optimization, to remove the need for checking every
cookie with a particular domain (which may involve reading many files).
The default implementations of domain_return_ok and path_return_ok
(return True) leave all the work to return_ok.
If domain_return_ok returns true for the cookie domain, path_return_ok
is called for the cookie path. Otherwise, path_return_ok and return_ok
are never called for that cookie domain. If path_return_ok returns
true, return_ok is called with the Cookie object itself for a full
check. Otherwise, return_ok is never called for that cookie path.
Note that domain_return_ok is called for every *cookie* domain, not
just for the *request* domain. For example, the function might be
called with both ".acme.com" and "www.acme.com" if the request domain
is "www.acme.com". The same goes for path_return_ok.
For argument documentation, see the docstring for return_ok.
"""
return True
def domain_return_ok(self, domain, request):
"""Return false if cookies should not be returned, given cookie domain.
This is here as an optimization, to remove the need for checking every
cookie with a particular domain (which may involve reading many files).
The default implementations of domain_return_ok and path_return_ok
(return True) leave all the work to return_ok.
If domain_return_ok returns true for the cookie domain, path_return_ok
is called for the cookie path. Otherwise, path_return_ok and return_ok
are never called for that cookie domain. If path_return_ok returns
true, return_ok is called with the Cookie object itself for a full
check. Otherwise, return_ok is never called for that cookie path.
Note that domain_return_ok is called for every *cookie* domain, not
just for the *request* domain. For example, the function might be
called with both ".acme.com" and "www.acme.com" if the request domain
is "www.acme.com". The same goes for path_return_ok.
For argument documentation, see the docstring for return_ok.
"""
return True
def test_firefox3_cookiejar_iteration(self):
try:
from mechanize import Firefox3CookieJar
except ImportError:
pass
else:
from mechanize import DefaultCookiePolicy
filename = self.mktemp()
hide_experimental_warnings()
try:
cj = Firefox3CookieJar(
filename, policy=DefaultCookiePolicy(rfc2965=True))
finally:
reset_experimental_warnings()
cj.connect()
self._interact(cj)
summary = "\n".join([str(cookie) for cookie in cj])
self.assertEquals(summary, """\
<Cookie foo2=bar for www.acme.com:80/>
<Cookie foo3=bar for www.acme.com/>
<Cookie foo1=bar for www.acme.com/>
<Cookie fooa=bar for www.foo.com/>
<Cookie foob=bar for .foo.com/>
<Cookie fooc=bar for .www.foo.com/>""")
def test_netscape_misc(self):
# Some additional Netscape cookies tests.
from mechanize import CookieJar, Request
c = CookieJar()
headers = []
req = Request("http://foo.bar.acme.com/foo")
# Netscape allows a host part that contains dots
headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com")
res = FakeResponse(headers, "http://www.acme.com/foo")
c.extract_cookies(res, req)
# and that the domain is the same as the host without adding a leading
# dot to the domain. Should not quote even if strange chars are used
# in the cookie value.
headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com")
res = FakeResponse(headers, "http://www.acme.com/foo")
c.extract_cookies(res, req)
req = Request("http://foo.bar.acme.com/foo")
c.add_cookie_header(req)
assert (req.get_header("Cookie").find("PART_NUMBER=3,4") != -1 and
req.get_header("Cookie").find("Customer=WILE_E_COYOTE") != -1)
def domain_return_ok(self, domain, request):
"""Return false if cookies should not be returned, given cookie domain.
This is here as an optimization, to remove the need for checking every
cookie with a particular domain (which may involve reading many files).
The default implementations of domain_return_ok and path_return_ok
(return True) leave all the work to return_ok.
If domain_return_ok returns true for the cookie domain, path_return_ok
is called for the cookie path. Otherwise, path_return_ok and return_ok
are never called for that cookie domain. If path_return_ok returns
true, return_ok is called with the Cookie object itself for a full
check. Otherwise, return_ok is never called for that cookie path.
Note that domain_return_ok is called for every *cookie* domain, not
just for the *request* domain. For example, the function might be
called with both ".acme.com" and "www.acme.com" if the request domain
is "www.acme.com". The same goes for path_return_ok.
For argument documentation, see the docstring for return_ok.
"""
return True
def domain_return_ok(self, domain, request):
"""Return false if cookies should not be returned, given cookie domain.
This is here as an optimization, to remove the need for checking every
cookie with a particular domain (which may involve reading many files).
The default implementations of domain_return_ok and path_return_ok
(return True) leave all the work to return_ok.
If domain_return_ok returns true for the cookie domain, path_return_ok
is called for the cookie path. Otherwise, path_return_ok and return_ok
are never called for that cookie domain. If path_return_ok returns
true, return_ok is called with the Cookie object itself for a full
check. Otherwise, return_ok is never called for that cookie path.
Note that domain_return_ok is called for every *cookie* domain, not
just for the *request* domain. For example, the function might be
called with both ".acme.com" and "www.acme.com" if the request domain
is "www.acme.com". The same goes for path_return_ok.
For argument documentation, see the docstring for return_ok.
"""
return True
def __str__(self):
if self.port is None: p = ""
else: p = ":"+self.port
limit = self.domain + p + self.path
if self.value is not None:
namevalue = "%s=%s" % (self.name, self.value)
else:
namevalue = self.name
return "<Cookie %s for %s>" % (namevalue, limit)
def __repr__(self):
args = []
for name in ["version", "name", "value",
"port", "port_specified",
"domain", "domain_specified", "domain_initial_dot",
"path", "path_specified",
"secure", "expires", "discard", "comment", "comment_url",
]:
attr = getattr(self, name)
args.append("%s=%s" % (name, repr(attr)))
args.append("rest=%s" % repr(self._rest))
args.append("rfc2109=%s" % repr(self.rfc2109))
return "Cookie(%s)" % ", ".join(args)
def set_ok(self, cookie, request):
"""Return true if (and only if) cookie should be accepted from server.
Currently, pre-expired cookies never get this far -- the CookieJar
class deletes such cookies itself.
cookie: mechanize.Cookie object
request: object implementing the interface defined by
CookieJar.extract_cookies.__doc__
"""
raise NotImplementedError()
def return_ok(self, cookie, request):
"""Return true if (and only if) cookie should be returned to server.
cookie: mechanize.Cookie object
request: object implementing the interface defined by
CookieJar.add_cookie_header.__doc__
"""
raise NotImplementedError()
def add_cookie_header(self, request):
"""Add correct Cookie: header to request (mechanize.Request object).
The Cookie2 header is also added unless policy.hide_cookie2 is true.
The request object (usually a mechanize.Request instance) must support
the methods get_full_url, get_host, is_unverifiable, get_type,
has_header, get_header, header_items and add_unredirected_header, as
documented by urllib2.
"""
debug("add_cookie_header")
cookies = self.cookies_for_request(request)
attrs = self._cookie_attrs(cookies)
if attrs:
if not request.has_header("Cookie"):
request.add_unredirected_header("Cookie", "; ".join(attrs))
# if necessary, advertise that we know RFC 2965
if self._policy.rfc2965 and not self._policy.hide_cookie2:
for cookie in cookies:
if cookie.version != 1 and not request.has_header("Cookie2"):
request.add_unredirected_header("Cookie2", '$Version="1"')
break
self.clear_expired_cookies()
def make_cookies(self, response, request):
"""Return sequence of Cookie objects extracted from response object.
See extract_cookies.__doc__ for the interface required of the
response and request arguments.
"""
self._policy._now = self._now = int(time.time())
return [cookie for cookie in self._make_cookies(response, request)
if cookie.expires is None or not cookie.expires <= self._now]
def set_cookie_if_ok(self, cookie, request):
"""Set a cookie if policy says it's OK to do so.
cookie: mechanize.Cookie instance
request: see extract_cookies.__doc__ for the required interface
"""
self._policy._now = self._now = int(time.time())
if self._policy.set_ok(cookie, request):
self.set_cookie(cookie)
def set_cookie(self, cookie):
"""Set a cookie, without checking whether or not it should be set.
cookie: mechanize.Cookie instance
"""
c = self._cookies
if not c.has_key(cookie.domain): c[cookie.domain] = {}
c2 = c[cookie.domain]
if not c2.has_key(cookie.path): c2[cookie.path] = {}
c3 = c2[cookie.path]
c3[cookie.name] = cookie
_clientcookie.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 15
收藏 0
点赞 0
评论 0
def __str__(self):
if self.port is None: p = ""
else: p = ":"+self.port
limit = self.domain + p + self.path
if self.value is not None:
namevalue = "%s=%s" % (self.name, self.value)
else:
namevalue = self.name
return "<Cookie %s for %s>" % (namevalue, limit)
_clientcookie.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
def __repr__(self):
args = []
for name in ["version", "name", "value",
"port", "port_specified",
"domain", "domain_specified", "domain_initial_dot",
"path", "path_specified",
"secure", "expires", "discard", "comment", "comment_url",
]:
attr = getattr(self, name)
args.append("%s=%s" % (name, repr(attr)))
args.append("rest=%s" % repr(self._rest))
args.append("rfc2109=%s" % repr(self.rfc2109))
return "Cookie(%s)" % ", ".join(args)
_clientcookie.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
def set_ok(self, cookie, request):
"""Return true if (and only if) cookie should be accepted from server.
Currently, pre-expired cookies never get this far -- the CookieJar
class deletes such cookies itself.
cookie: mechanize.Cookie object
request: object implementing the interface defined by
CookieJar.extract_cookies.__doc__
"""
raise NotImplementedError()
_clientcookie.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
def return_ok(self, cookie, request):
"""Return true if (and only if) cookie should be returned to server.
cookie: mechanize.Cookie object
request: object implementing the interface defined by
CookieJar.add_cookie_header.__doc__
"""
raise NotImplementedError()
_clientcookie.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def add_cookie_header(self, request):
"""Add correct Cookie: header to request (mechanize.Request object).
The Cookie2 header is also added unless policy.hide_cookie2 is true.
The request object (usually a mechanize.Request instance) must support
the methods get_full_url, get_host, is_unverifiable, get_type,
has_header, get_header, header_items and add_unredirected_header, as
documented by urllib2.
"""
debug("add_cookie_header")
cookies = self.cookies_for_request(request)
attrs = self._cookie_attrs(cookies)
if attrs:
if not request.has_header("Cookie"):
request.add_unredirected_header("Cookie", "; ".join(attrs))
# if necessary, advertise that we know RFC 2965
if self._policy.rfc2965 and not self._policy.hide_cookie2:
for cookie in cookies:
if cookie.version != 1 and not request.has_header("Cookie2"):
request.add_unredirected_header("Cookie2", '$Version="1"')
break
self.clear_expired_cookies()
_clientcookie.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def make_cookies(self, response, request):
"""Return sequence of Cookie objects extracted from response object.
See extract_cookies.__doc__ for the interface required of the
response and request arguments.
"""
self._policy._now = self._now = int(time.time())
return [cookie for cookie in self._make_cookies(response, request)
if cookie.expires is None or not cookie.expires <= self._now]
_clientcookie.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 16
收藏 0
点赞 0
评论 0
def set_cookie_if_ok(self, cookie, request):
"""Set a cookie if policy says it's OK to do so.
cookie: mechanize.Cookie instance
request: see extract_cookies.__doc__ for the required interface
"""
self._policy._now = self._now = int(time.time())
if self._policy.set_ok(cookie, request):
self.set_cookie(cookie)
_clientcookie.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def set_cookie(self, cookie):
"""Set a cookie, without checking whether or not it should be set.
cookie: mechanize.Cookie instance
"""
c = self._cookies
if not c.has_key(cookie.domain): c[cookie.domain] = {}
c2 = c[cookie.domain]
if not c2.has_key(cookie.path): c2[cookie.path] = {}
c3 = c2[cookie.path]
c3[cookie.name] = cookie
def __str__(self):
if self.port is None: p = ""
else: p = ":"+self.port
limit = self.domain + p + self.path
if self.value is not None:
namevalue = "%s=%s" % (self.name, self.value)
else:
namevalue = self.name
return "<Cookie %s for %s>" % (namevalue, limit)
def __repr__(self):
args = []
for name in ["version", "name", "value",
"port", "port_specified",
"domain", "domain_specified", "domain_initial_dot",
"path", "path_specified",
"secure", "expires", "discard", "comment", "comment_url",
]:
attr = getattr(self, name)
args.append("%s=%s" % (name, repr(attr)))
args.append("rest=%s" % repr(self._rest))
args.append("rfc2109=%s" % repr(self.rfc2109))
return "Cookie(%s)" % ", ".join(args)
def set_ok(self, cookie, request):
"""Return true if (and only if) cookie should be accepted from server.
Currently, pre-expired cookies never get this far -- the CookieJar
class deletes such cookies itself.
cookie: mechanize.Cookie object
request: object implementing the interface defined by
CookieJar.extract_cookies.__doc__
"""
raise NotImplementedError()
def return_ok(self, cookie, request):
"""Return true if (and only if) cookie should be returned to server.
cookie: mechanize.Cookie object
request: object implementing the interface defined by
CookieJar.add_cookie_header.__doc__
"""
raise NotImplementedError()
def add_cookie_header(self, request):
"""Add correct Cookie: header to request (mechanize.Request object).
The Cookie2 header is also added unless policy.hide_cookie2 is true.
The request object (usually a mechanize.Request instance) must support
the methods get_full_url, get_host, is_unverifiable, get_type,
has_header, get_header, header_items and add_unredirected_header, as
documented by urllib2.
"""
debug("add_cookie_header")
cookies = self.cookies_for_request(request)
attrs = self._cookie_attrs(cookies)
if attrs:
if not request.has_header("Cookie"):
request.add_unredirected_header("Cookie", "; ".join(attrs))
# if necessary, advertise that we know RFC 2965
if self._policy.rfc2965 and not self._policy.hide_cookie2:
for cookie in cookies:
if cookie.version != 1 and not request.has_header("Cookie2"):
request.add_unredirected_header("Cookie2", '$Version="1"')
break
self.clear_expired_cookies()
def make_cookies(self, response, request):
"""Return sequence of Cookie objects extracted from response object.
See extract_cookies.__doc__ for the interface required of the
response and request arguments.
"""
self._policy._now = self._now = int(time.time())
return [cookie for cookie in self._make_cookies(response, request)
if cookie.expires is None or not cookie.expires <= self._now]
def set_cookie_if_ok(self, cookie, request):
"""Set a cookie if policy says it's OK to do so.
cookie: mechanize.Cookie instance
request: see extract_cookies.__doc__ for the required interface
"""
self._policy._now = self._now = int(time.time())
if self._policy.set_ok(cookie, request):
self.set_cookie(cookie)