本文整理匯總了Python中urllib.request.header_items方法的典型用法代碼示例。如果您正苦於以下問題:Python request.header_items方法的具體用法?Python request.header_items怎麽用?Python request.header_items使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類urllib.request
的用法示例。
在下文中一共展示了request.header_items方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_custom_headers
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import header_items [as 別名]
def test_custom_headers(self):
url = "http://www.example.com"
with support.transient_internet(url):
opener = urllib.request.build_opener()
request = urllib.request.Request(url)
self.assertFalse(request.header_items())
opener.open(request)
self.assertTrue(request.header_items())
self.assertTrue(request.has_header('User-agent'))
request.add_header('User-Agent','Test-Agent')
opener.open(request)
self.assertEqual(request.get_header('User-agent'),'Test-Agent')
示例2: http_request
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import header_items [as 別名]
def http_request(self, request):
host, full_url = request.host, request.get_full_url()
url_path = full_url[full_url.find(host) + len(host):]
log_url ( self.log, "Requesting: ", request.get_full_url(), TRACE_LEVEL )
self.log.log(self.log_level, "%s %s" % (request.get_method(), url_path))
for header in request.header_items():
self.log.log(self.log_level, " . %s: %s" % header[:])
return request
示例3: test_custom_headers
# 需要導入模塊: from urllib import request [as 別名]
# 或者: from urllib.request import header_items [as 別名]
def test_custom_headers(self):
url = support.TEST_HTTP_URL
with support.transient_internet(url):
opener = urllib.request.build_opener()
request = urllib.request.Request(url)
self.assertFalse(request.header_items())
opener.open(request)
self.assertTrue(request.header_items())
self.assertTrue(request.has_header('User-agent'))
request.add_header('User-Agent','Test-Agent')
opener.open(request)
self.assertEqual(request.get_header('User-agent'),'Test-Agent')