如何使用 python urllib 忽略 Windows 代理设置? [英] How to ignore windows proxy settings with python urllib?

查看:106
本文介绍了如何使用 python urllib 忽略 Windows 代理设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 Python 在使用 urllib 时忽略 Windows 代理设置.我设法做到这一点的唯一方法是禁用 Internet Explorer 上的所有代理设置.有什么程序化的方式吗?

I want Python to ignore Windows proxy settings when using urllib. The only way I managed to do that was disabling all proxy settings on Internet Explorer. Is there any programmatic way?

os.environ['no_proxy'] 不是一个好的选择,因为我想避免所有地址的代理.

os.environ['no_proxy'] is not a good option, since I'd like to avoid proxy for all addresses.

推荐答案

来自 urlib2 文档:类 urllib2.ProxyHandler([proxies]) ... 要禁用自动检测到的代理,请传递一个空字典.

From the urlib2 documentation: Class urllib2.ProxyHandler([proxies]) ... To disable autodetected proxy pass an empty dictionary.

所以我们要做的是:

import urllib2
proxy = urllib2.ProxyHandler({}) # Pass empty dictionary to bypass proxy
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
socket = urllib2.urlopen('http://www.google.com')
content = socket.read() 

这篇关于如何使用 python urllib 忽略 Windows 代理设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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