python-requests经过身份验证的代理httplib.BadStatusLine

时间:2013-08-02 15:07:17

标签: python curl proxy python-requests

通过python-requests验证的代理返回以下错误:

>>> import requests
>>> proxies = {'https': 'http://username:password@proxy.company.com:8080',}
>>> requests.get('https://api.github.com/',proxies=proxies,verify=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/usr/lib/python2.6/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.6/site-packages/requests/sessions.py", line 335, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.6/site-packages/requests/sessions.py", line 438, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.6/site-packages/requests/adapters.py", line 327, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='proxy.company.com', port=8080): Max retries exceeded with url: https://api.github.com/ (Caused by <class 'httplib.BadStatusLine'>: )

经过身份验证的代理在curl中运行良好:

$ curl --proxy-user username:password --proxy http://proxy.company.com:8080 -k https://api.github.com/
{
  "current_user_url": "https://api.github.com/user",
  "authorizations_url": "https://api.github.com/authorizations",
  "emails_url": "https://api.github.com/user/emails",
  "emojis_url": "https://api.github.com/emojis",
  "events_url": "https://api.github.com/events",
  "feeds_url": "https://api.github.com/feeds",
  "following_url": "https://api.github.com/user/following{/target}",
  "gists_url": "https://api.github.com/gists{/gist_id}",
  "hub_url": "https://api.github.com/hub",
  "issue_search_url": "https://api.github.com/legacy/issues/search/{owner}/{repo}/{state}/{keyword}",
  "issues_url": "https://api.github.com/issues",
  "keys_url": "https://api.github.com/user/keys",
  "notifications_url": "https://api.github.com/notifications",
  "organization_repositories_url": "https://api.github.com/orgs/{org}/repos/{?type,page,per_page,sort}",
  "organization_url": "https://api.github.com/orgs/{org}",
  "public_gists_url": "https://api.github.com/gists/public",
  "rate_limit_url": "https://api.github.com/rate_limit",
  "repository_url": "https://api.github.com/repos/{owner}/{repo}",
  "repository_search_url": "https://api.github.com/legacy/repos/search/{keyword}{?language,start_page}",
  "current_user_repositories_url": "https://api.github.com/user/repos{?type,page,per_page,sort}",
  "starred_url": "https://api.github.com/user/starred{/owner}{/repo}",
  "starred_gists_url": "https://api.github.com/gists/starred",
  "team_url": "https://api.github.com/teams",
  "user_url": "https://api.github.com/users/{user}",
  "user_organizations_url": "https://api.github.com/user/orgs",
  "user_repositories_url": "https://api.github.com/users/{user}/repos{?type,page,per_page,sort}",
  "user_search_url": "https://api.github.com/legacy/user/search/{keyword}"
}

如何解决问题?

2 个答案:

答案 0 :(得分:1)

自2.0以来,请求一直支持HTTP代理:https://github.com/kennethreitz/requests/pull/1515

顺便说一句,它确实尊重代理环境变量,所以你可以设置:

export http_proxy="http://username:password@proxy.company.com:8080"
export https_proxy=$http_proxy

有关请求1.x中代理支持状态的更多详细信息,请参阅此博客文章:https://lukasa.co.uk/2013/07/Python_Requests_And_Proxies/

答案 1 :(得分:0)

您的代理确实是HTTP而不是HTTPS。要使用HTTPS代理,它需要使用连接协议,而不是HTTP,可能使用CONNECT基本身份验证。现在我没有可用auth的HTTPS代理,也不知道如何安装这样的系统,但如果你只有"https://username:password@proxy.company.com:8080"会发生什么呢?

另一方面,您可能希望确保requests / urllib3是最新的。 See this bug report