请求库的 Python 代理错误 [英] Python Proxy Error With Requests Library

查看:22
本文介绍了请求库的 Python 代理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Python 中的代理服务器访问网络.我正在使用 requests 库,但在验证我的代理时遇到问题,因为我使用的代理需要密码.

I am trying to access the web via a proxy server in Python. I am using the requests library and I am having an issue with authenticating my proxy as the proxy I am using requires a password.

proxyDict = { 
          'http'  : 'username:mypassword@77.75.105.165', 
          'https' : 'username:mypassword@77.75.105.165'
        }
r = requests.get("http://www.google.com", proxies=proxyDict)

我收到以下错误:

Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
r = requests.get("http://www.google.com", proxies=proxyDict)
File "C:Python27libsite-packages
equestsapi.py", line 78, in get
:param url: URL for the new :class:`Request` object.
File "C:Python27libsite-packages
equestsapi.py", line 65, in request
"""Sends a POST request. Returns :class:`Response` object.
File "C:Python27libsite-packages
equestssessions.py", line 187, in request
def head(self, url, **kwargs):
File "C:Python27libsite-packages
equestsmodels.py", line 407, in send
"""
File "C:Python27libsite-packages
equestspackagesurllib3poolmanager.py", line     127, in proxy_from_url
File "C:Python27libsite-packages
equestspackagesurllib3connectionpool.py", line    521, in connection_from_url
File "C:Python27libsite-packages
equestspackagesurllib3connectionpool.py", line 497, in get_host
ValueError: invalid literal for int() with base 10: 'h6f2v6jh5dsxa@77.75.105.165'

我该如何解决这个问题?

How do I solve this?

预先感谢您的帮助.

推荐答案

您应该从 proxyDict 中删除嵌入的用户名和密码,并使用 auth 参数代替.

You should remove the embedded username and password from proxyDict, and use the auth parameter instead.

import requests
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)

这篇关于请求库的 Python 代理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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