python http代理_python 怎样设置代理访问http请求?

使用Python3.2.2进行HTTP请求时遇到407 Proxy Authentication Required错误,示例代码中包含了设置代理和认证信息的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用python 3.2.2

按照文档示例,如下:

authinfo = urllib.request.HTTPBasicAuthHandler()

authinfo.add_password(realm='PDQ Application',

uri='https://mahler:8092/site-updates.py',

user='klem',

passwd='geheim$parole')

proxy_support = urllib.request.ProxyHandler({"http" : "http://ahad-haam:3128"})

# build a new opener that adds authentication and caching FTP handlers

opener = urllib.request.build_opener(proxy_support, authinfo,

urllib.request.CacheFTPHandler)

# install it

urllib.request.install_opener(opener)

f = urllib.request.urlopen('http://www.python.org/')

访问的时候报

Traceback (most recent call last):

File "", line 1, in

File "D:\Python\Python32\lib\urllib\request.py", line 138, in urlopen

return opener.open(url, data, timeout)

File "D:\Python\Python32\lib\urllib\request.py", line 375, in open

response = meth(req, response)

File "D:\Python\Python32\lib\urllib\request.py", line 487, in http_response

'http', request, response, code, msg, hdrs)

File "D:\Python\Python32\lib\urllib\request.py", line 413, in error

return self._call_chain(*args)

File "D:\Python\Python32\lib\urllib\request.py", line 347, in _call_chain

result = func(*args)

File "D:\Python\Python32\lib\urllib\request.py", line 495, in http_error_default

raise HTTPError(req.full_url, code, msg, hdrs, fp)

urllib.error.HTTPError: HTTP Error 407: Proxy Authentication Required

有谁知道怎么解决吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值