使用Python Requests时代理返回407响应

3
以下是我使用的代码。我正在使用最新的Python requests。在使用python 2.7时,我从下面的请求中获得407响应。 奇怪的是,当我在请求中使用https而不是http时,我会得到503响应。
response = requests.get(query, proxies={'https': "https://username:password@104.247.XX.XX:80"}, headers=headers, timeout=30, allow_redirects=True)
print response

输出:响应[503]

response = requests.get(query, proxies={'http': "http://username:password@104.247.XX.XX:80"}, headers=headers, timeout=30, allow_redirects=True)
print response

输出:响应[407]

但是相同的代码在我的Amazon EC2实例上可以正常工作,尽管我正在尝试在本地计算机上运行。

import urllib2
import urllib
import portalocker
import cookielib
import requests

query = 'http://google.com/search?q=wtf&num=100&tbs=cdr:1,cd_min:2000,cd_max:2015&start=0&filter=0'
headers = {'user-agent': 'Mozilla/5.0 (X11; Linux; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 Midori/0.4'}
response = requests.get(query, proxies={'http': "http://username:password@104.247.XX.XX:80"}, headers=headers, timeout=30, allow_redirects=True)
print response
2个回答

4
from requests.auth import HTTPProxyAuth

proxyDict = { 
          'http'  : '77.75.105.165', 
          'https' : '77.75.105.165'
        }
auth = HTTPProxyAuth('username', 'mypassword')

r = requests.get("http://www.google.com", proxies=proxyDict, auth=auth)

3
请查看如何撰写好的答案,仅发布代码而不解释并不是很有用。 - Nuageux

-1
状态码可能会给出一些线索:
407 Proxy Authentication Required
503 Service Unavailable

这些提示表明您的代理服务器未运行 https,并且您正在使用的代理服务器的用户名/密码组合是错误的。请注意,您本地计算机需要与 ec2 实例使用相同的代理服务器的可能性非常小。


用户名和密码组合是100%正确的。 我从http得到407,从https得到503。之前我从https得到了200。 - CodeNinja101

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接