需要代理身份验证

需要代理身份验证

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

问题描述

我正在尝试使用 urllib.request.urlopen() 打开一个网站(我在公司代理后面),但出现错误:

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

我可以在 urllib.request.getproxies() 中找到代理,但是如何指定用于它的用户名和密码?我在官方文档中找不到解决方案.

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:需要代理身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 08:42