本文整理汇总了Python中urllib2.Request.get_host方法的典型用法代码示例。如果您正苦于以下问题:Python Request.get_host方法的具体用法?Python Request.get_host怎么用?Python Request.get_host使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2.Request
的用法示例。
在下文中一共展示了Request.get_host方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_proxy_https
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_host [as 别名]
def test_proxy_https(self):
o = OpenerDirector()
ph = urllib2.ProxyHandler(dict(https="proxy.example.com:3128"))
o.add_handler(ph)
meth_spec = [[("https_open", "return response")]]
handlers = add_ordered_mock_handlers(o, meth_spec)
req = Request("https://www.example.com/")
self.assertEqual(req.get_host(), "www.example.com")
r = o.open(req)
self.assertEqual(req.get_host(), "proxy.example.com:3128")
self.assertEqual([(handlers[0], "https_open")], [tup[0:2] for tup in o.calls])
示例2: get_host
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_host [as 别名]
def get_host(self):
_Request.get_host(self)
if self.port is None:
if self.get_type() == 'http':
default = 80
elif self.get_type() == 'https':
default = 443
else:
raise ValueError('Unsupported type.', self.get_type())
self.host, self.port = splitnport(self.host, default)
return self.host
示例3: test_proxy_no_proxy
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_host [as 别名]
def test_proxy_no_proxy(self):
os.environ['no_proxy'] = 'python.org'
o = OpenerDirector()
ph = urllib2.ProxyHandler(dict(http="proxy.example.com"))
o.add_handler(ph)
req = Request("http://www.perl.org/")
self.assertEqual(req.get_host(), "www.perl.org")
r = o.open(req)
self.assertEqual(req.get_host(), "proxy.example.com")
req = Request("http://www.python.org")
self.assertEqual(req.get_host(), "www.python.org")
r = o.open(req)
self.assertEqual(req.get_host(), "www.python.org")
del os.environ['no_proxy']
示例4: sign_request
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_host [as 别名]
def sign_request(request, secret_key):
req = Request(request)
raw = "%s\n%s\n%s\n%s" % (req.get_method(),req.get_host(),req.get_selector().split('?')[0],req.get_selector().split('?')[1])
key = bytes(secret_key)
print(raw)
hashed = hmac.new(key, raw, sha1)
signature = binascii.b2a_base64(hashed.digest())
return request + '&signature=%s' % quote(signature, '\n')
示例5: test_proxy_https_proxy_authorization
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_host [as 别名]
def test_proxy_https_proxy_authorization(self):
o = OpenerDirector()
ph = urllib2.ProxyHandler(dict(https="proxy.example.com:3128"))
o.add_handler(ph)
https_handler = MockHTTPSHandler()
o.add_handler(https_handler)
req = Request("https://www.example.com/")
req.add_header("Proxy-Authorization", "FooBar")
req.add_header("User-Agent", "Grail")
self.assertEqual(req.get_host(), "www.example.com")
self.assertIsNone(req._tunnel_host)
r = o.open(req)
# Verify Proxy-Authorization gets tunneled to request.
# httpsconn req_headers do not have the Proxy-Authorization header but
# the req will have.
self.assertNotIn(("Proxy-Authorization", "FooBar"), https_handler.httpconn.req_headers)
self.assertIn(("User-Agent", "Grail"), https_handler.httpconn.req_headers)
self.assertIsNotNone(req._tunnel_host)
self.assertEqual(req.get_host(), "proxy.example.com:3128")
self.assertEqual(req.get_header("Proxy-authorization"), "FooBar")
示例6: test_wrapped_url
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import get_host [as 别名]
def test_wrapped_url(self):
req = Request("<URL:http://www.python.org>")
self.assertEqual("www.python.org", req.get_host())