Python的3​​

编程入门 行业动态 更新时间:2024-10-20 03:48:31
本文介绍了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('google)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'username:password@url:port'}) auth = req.HTTPBasicAuthHandler() opener = req.build_opener(proxy, auth, req.HTTPHandler) req.install_opener(opener) conn = req.urlopen('google') return_str = conn.read()

更多推荐

Python的3​​

本文发布于:2023-11-08 07:18:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!