繁体   English   中英

代理后面的Python请求

[英]Python Requests behind proxy

我在公司代理(Isa服务器)后面。

使用urllib2时,我可以毫无问题地通过代理连接到Internet,但是使用请求库时,我不能。

这是我的urllib2代码:

proxy = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

page = urllib2.urlopen('http://www.google.com')
print page.getcode()

这将打印“ 200”并正常工作

但是,对请求执行相同操作时,我得到了407代码,无法正常工作。

proxy_dict = {
    'http': 'http://10.20.23.5:8080',
    'https': 'ftp://10.20.23.5:8080',
    'ftp': 'https://10.20.23.5:8080'
}

page = requests.get('http://www.google.com', proxies=proxy_dict)
print page.status_code
print page.reason

这将显示“ 407”,并显示原因:“需要代理身份验证(Forefront TMG需要授权才能满足该请求。拒绝访问Web代理过滤器。”)

即使我传递给urllib2的代理请求也不起作用:

page = requests.get('http://http://www.google.com', proxies=urllib2.getproxies())

Urllib2正在执行请求未执行的操作。

有帮助吗?

如果您的代理服务器需要身份验证,则需要设置以下变量:

proxy_dict = {
    'http': 'http://username:password@10.20.23.5:8080',
    'https': 'https://username:password@10.20.23.5:8080',
    'ftp': 'ftp://username:password@10.20.23.5:8080'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM