本文整理汇总了Python中cookielib.request_host方法的典型用法代码示例。如果您正苦于以下问题:Python cookielib.request_host方法的具体用法?Python cookielib.request_host怎么用?Python cookielib.request_host使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cookielib
的用法示例。
在下文中一共展示了cookielib.request_host方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_request_host
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import request_host [as 别名]
def test_request_host(self):
from urllib2 import Request
from cookielib import request_host
# this request is illegal (RFC2616, 14.2.3)
req = Request("http://1.1.1.1/",
headers={"Host": "www.acme.com:80"})
# libwww-perl wants this response, but that seems wrong (RFC 2616,
# section 5.2, point 1., and RFC 2965 section 1, paragraph 3)
#self.assertEqual(request_host(req), "www.acme.com")
self.assertEqual(request_host(req), "1.1.1.1")
req = Request("http://www.acme.com/",
headers={"Host": "irrelevant.com"})
self.assertEqual(request_host(req), "www.acme.com")
# not actually sure this one is valid Request object, so maybe should
# remove test for no host in url in request_host function?
req = Request("/resource.html",
headers={"Host": "www.acme.com"})
self.assertEqual(request_host(req), "www.acme.com")
# port shouldn't be in request-host
req = Request("http://www.acme.com:2345/resource.html",
headers={"Host": "www.acme.com:5432"})
self.assertEqual(request_host(req), "www.acme.com")
示例2: test_request_host
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import request_host [as 别名]
def test_request_host(self):
from urllib2 import Request
from cookielib import request_host
# this request is illegal (RFC2616, 14.2.3)
req = Request("http://1.1.1.1/",
headers={"Host": "www.acme.com:80"})
# libwww-perl wants this response, but that seems wrong (RFC 2616,
# section 5.2, point 1., and RFC 2965 section 1, paragraph 3)
#self.assertEquals(request_host(req), "www.acme.com")
self.assertEquals(request_host(req), "1.1.1.1")
req = Request("http://www.acme.com/",
headers={"Host": "irrelevant.com"})
self.assertEquals(request_host(req), "www.acme.com")
# not actually sure this one is valid Request object, so maybe should
# remove test for no host in url in request_host function?
req = Request("/resource.html",
headers={"Host": "www.acme.com"})
self.assertEquals(request_host(req), "www.acme.com")
# port shouldn't be in request-host
req = Request("http://www.acme.com:2345/resource.html",
headers={"Host": "www.acme.com:5432"})
self.assertEquals(request_host(req), "www.acme.com")
示例3: get_origin_req_host
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import request_host [as 别名]
def get_origin_req_host(self):
if self.origin_req_host is None:
return request_host(self.host)
else:
return self.origin_req_host
示例4: __init__
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import request_host [as 别名]
def __init__(self, url, data = None, method = None, headers = {}, origin_req_host = None, unverifiable = False,
rawurl = False):
'''
:param url: request url
:param data: request data, can be a str/bytes, or a stream(vlcp.event.stream.XXXStream)
:param method: request method (GET, POST, ...)
:param headers: request header dict ({'user-agent':'myagent'})
:param origin_req_host: origin request host for cookie policy check
:param unverifiable: unverifiable for cookie policy check
'''
self.url = _str(url, 'ascii')
s = urlsplit(self.url, 'http')
self.type = 'https' if s.scheme == 'https' else 'http'
self.host = s.netloc
if not self.host:
raise ValueError('Invalid URL: ' + self.url)
if rawurl:
self.path = urlunsplit(('', '', s.path, s.query, ''))
else:
self.path = urlunsplit(('', '', quote(s.path), quote(s.query,'/&='), ''))
if not self.path:
self.path = '/'
self.data = data
if method is None:
if self.data is None:
self.method = 'GET'
else:
self.method = 'POST'
else:
self.method = method.upper()
if self.data is not None:
if isinstance(self.data, unicode):
self.data = _bytes(self.data)
headers = dict(headers)
self.headers = dict((_str(k), _str(v, 'iso-8859-1')) for k,v in headers.items())
self.headerdict = dict((k.lower(), v) for k,v in self.headers.items())
self.headermap = dict((k.lower(), k) for k in self.headers.keys())
self.undirectedheaders = {}
self.undirectedheaderdict = {}
self.undirectedheadermap = {}
self.hostname = request_host(self)
if origin_req_host is None:
origin_req_host = request_host(self)
self.origin_req_host = origin_req_host
self.unverifiable = unverifiable
self.redirect_count = 0
if self.data and not self.has_header('Content-Type'):
self.add_header('Content-Type', 'application/x-www-form-urlencoded')