本文整理汇总了Python中urllib2.Request.set_proxy方法的典型用法代码示例。如果您正苦于以下问题:Python Request.set_proxy方法的具体用法?Python Request.set_proxy怎么用?Python Request.set_proxy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2.Request
的用法示例。
在下文中一共展示了Request.set_proxy方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_http_doubleslash
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import set_proxy [as 别名]
def test_http_doubleslash(self):
# Checks that the presence of an unnecessary double slash in a url doesn't break anything
# Previously, a double slash directly after the host could cause incorrect parsing of the url
h = urllib2.AbstractHTTPHandler()
o = h.parent = MockOpener()
data = ""
ds_urls = [
"http://example.com/foo/bar/baz.html",
"http://example.com//foo/bar/baz.html",
"http://example.com/foo//bar/baz.html",
"http://example.com/foo/bar//baz.html",
]
for ds_url in ds_urls:
ds_req = Request(ds_url, data)
# Check whether host is determined correctly if there is no proxy
np_ds_req = h.do_request_(ds_req)
self.assertEqual(np_ds_req.unredirected_hdrs["Host"],"example.com")
# Check whether host is determined correctly if there is a proxy
ds_req.set_proxy("someproxy:3128",None)
p_ds_req = h.do_request_(ds_req)
self.assertEqual(p_ds_req.unredirected_hdrs["Host"],"example.com")
示例2: call_hipchat
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import set_proxy [as 别名]
def call_hipchat(cls, ReturnType, url='', urlv2='', data=True, **kw):
auth = [('format', 'json'), ('auth_token', hipchat.config.token)]
if hipchat.config.api_version == 2:
url=urlv2
if not data:
auth.extend(kw.items())
req = Request(url=hipchat.config.api_url + url + '?%s' % urlencode(auth))
if data:
req.add_data(urlencode(kw.items()))
if hipchat.config.proxy_server and hipchat.config.proxy_type:
req.set_proxy(hipchat.config.proxy_server, hipchat.config.proxy_type)
try:
res = urlopen(req)
except Exception, e:
resp = "".join(e.readlines())
try:
err_resp = json.loads(resp)
except ValueError:
raise Exception(
"unknown error: %d response was: %s" % (
e.getcode(), resp
),
)
error = err_resp.get("error", {})
raise Exception(
"%d %s error: %s" % (
error.get("code", -1),
error.get("type", "unknown"),
error.get("message", "no message"),
)
)
示例3: call_hipchat
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import set_proxy [as 别名]
def call_hipchat(cls, ReturnType, url, data=True, **kw):
auth = [('format', 'json'), ('auth_token', hipchat.config.token)]
if not data:
auth.extend(kw.items())
req = Request(url=url + '?%s' % urlencode(auth))
if data:
req.add_data(urlencode(kw.items()))
if hipchat.config.proxy_server and hipchat.config.proxy_type:
req.set_proxy(hipchat.config.proxy_server, hipchat.config.proxy_type)
return ReturnType(json.load(urlopen(req)))