Python的3​​ - 的urllib,HTTP错误407:需要代理身份验证 [英] Python 3 - urllib, HTTP Error 407: Proxy Authentication Required

查看:3428
本文介绍了Python的3​​ - 的urllib,HTTP错误407:需要代理身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开一个网站(我后面的企业代理)使用urllib.request.urlopen(),但我得到的错误:

  urllib.error.HTTPError:HTTP错误407:需要代理身份验证

我可以找到urllib.request.getproxies()代理,但如何指定用户名和密码才能使用呢?我找不到在官方文档解决方案。


解决方案

 导入urllib.request里的REQ代理= req.ProxyHandler({'HTTP':r'http://用户名:密码@网址:口'})
AUTH = req.HTTPBasicAuthHandler()
首战= req.build_opener(代理,身份验证,req.HTTPHandler)
req.install_opener(揭幕战)
康恩= req.urlopen('http://google.com)
return_str = conn.read()

I'm trying to open a website (I am behind a corporate proxy) using urllib.request.urlopen() but I am getting the error:

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

I can find the proxy in urllib.request.getproxies(), but how do I specify a username and password to use for it? I couldn't find the solution in the official docs.

解决方案

import urllib.request as req

proxy = req.ProxyHandler({'http': r'http://username:password@url:port'})
auth = req.HTTPBasicAuthHandler()
opener = req.build_opener(proxy, auth, req.HTTPHandler)
req.install_opener(opener)
conn = req.urlopen('http://google.com')
return_str = conn.read()

这篇关于Python的3​​ - 的urllib,HTTP错误407:需要代理身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆