请求库的 Python 代理错误

发布于 2024-12-27 06:15:56 字数 1772 浏览 1 评论 0原文

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

proxyDict = { 
          'http'  : 'username:[email protected]', 
          'https' : 'username:[email protected]'
        }
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:\Python27\lib\site-packages\requests\api.py", line 78, in get
:param url: URL for the new :class:`Request` object.
File "C:\Python27\lib\site-packages\requests\api.py", line 65, in request
"""Sends a POST request. Returns :class:`Response` object.
File "C:\Python27\lib\site-packages\requests\sessions.py", line 187, in request
def head(self, url, **kwargs):
File "C:\Python27\lib\site-packages\requests\models.py", line 407, in send
"""
File "C:\Python27\lib\site-packages\requests\packages\urllib3\poolmanager.py", line     127, in proxy_from_url
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line    521, in connection_from_url
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 497, in get_host
ValueError: invalid literal for int() with base 10: '[email protected]'

我该如何解决这个问题?

预先感谢您的帮助。

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:[email protected]', 
          'https' : 'username:[email protected]'
        }
r = requests.get("http://www.google.com", proxies=proxyDict)

I am getting the following error:

Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
r = requests.get("http://www.google.com", proxies=proxyDict)
File "C:\Python27\lib\site-packages\requests\api.py", line 78, in get
:param url: URL for the new :class:`Request` object.
File "C:\Python27\lib\site-packages\requests\api.py", line 65, in request
"""Sends a POST request. Returns :class:`Response` object.
File "C:\Python27\lib\site-packages\requests\sessions.py", line 187, in request
def head(self, url, **kwargs):
File "C:\Python27\lib\site-packages\requests\models.py", line 407, in send
"""
File "C:\Python27\lib\site-packages\requests\packages\urllib3\poolmanager.py", line     127, in proxy_from_url
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line    521, in connection_from_url
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 497, in get_host
ValueError: invalid literal for int() with base 10: '[email protected]'

How do I solve this?

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对风讲故事 2025-01-03 06:15:56

您应该从 proxyDict 中删除嵌入的用户名和密码,并改用 auth 参数。

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)

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)
草莓味的萝莉 2025-01-03 06:15:56

我在 Windows 上遇到了类似的问题,并发现让请求正常工作的唯一方法是在启动 Python 之前将代理设置为环境变量。对于您来说,这将是这样的:

set HTTP_PROXY=http://77.75.105.165
set HTTPS_PROXY=https://77.75.105.165

您可能还想检查是否需要特定端口,如果需要,请将其设置在 url 之后。例如,如果端口是 8443,则执行以下操作:

set HTTP_PROXY=http://77.75.105.165:8443
set HTTPS_PROXY=https://77.75.105.165:8443

I've been having a similar problem on Windows and found the only way to get requests to work was to set the proxies as environment variables before I started Python. For you this would be something like this:

set HTTP_PROXY=http://77.75.105.165
set HTTPS_PROXY=https://77.75.105.165

You might also want to check is there's a specific port required, and if so set it after the url. For example, if the port is 8443 then do:

set HTTP_PROXY=http://77.75.105.165:8443
set HTTPS_PROXY=https://77.75.105.165:8443

您可以使用 urllib 库来实现此目的。

from urllib import request
request.urlopen("your URL", proxies=request.getproxies())

You can use urllib library for this.

from urllib import request
request.urlopen("your URL", proxies=request.getproxies())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文