如何处理 urllib3 中的代理 [英] How to handle proxies in urllib3

查看:51
本文介绍了如何处理 urllib3 中的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到有关如何在 urllib3 中构建简单脚本的可靠示例,该脚本打开一个 url(通过代理),然后读取它并最终打印它.代理需要用户/通行证才能进行身份验证,但是我不清楚您是如何做到这一点的?任何帮助将不胜感激.

I am having trouble finding solid examples of how to build a simple script in urllib3 which opens a url (via a proxy), then reads it and finally prints it. The proxy requires a user/pass to authenticate however it's not clear to me how you do this? Any help would be appreciated.

推荐答案

urllib3 有一个 <您可以使用的 code>ProxyManager 组件.您需要为 Basic Auth 组件构建标头,您可以手动完成,也可以使用 make_headers urllib3 中的帮助器.

urllib3 has a ProxyManager component which you can use. You'll need to build headers for the Basic Auth component, you can either do that manually or use the make_headers helper in urllib3.

放在一起,它看起来像这样:

All together, it would look something like this:

from urllib3 import ProxyManager, make_headers

default_headers = make_headers(proxy_basic_auth='myusername:mypassword')
http = ProxyManager("https://myproxy.com:8080/", proxy_headers=default_headers)

# Now you can use `http` as you would a normal PoolManager
r = http.request('GET', 'https://stackoverflow.com/')

这篇关于如何处理 urllib3 中的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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