简体   繁体   English

Python3 请求代理 (Socks5)

[英]Python3 Requests Proxy (Socks5)

Edit: I don't know if this is a thing, but often this happens when i've been testing a lot and I have a lot of python scripts running.编辑:我不知道这是否是一回事,但是当我进行了很多测试并且我有很多 python 脚本正在运行时,通常会发生这种情况。 Usually none active at the same time but I'll have ran a few of them in a short space of time.通常没有人同时处于活动状态,但我会在很短的时间内运行其中的一些。 I don't know if that's why teh error starts happening.我不知道这是否是错误开始发生的原因。

Ok here's my dilemma.好的,这是我的困境。 I've got a NordVPN account and I want to randomly loop through the ip's for the requests i'm making to google.我有一个 NordVPN 帐户,我想随机循环访问我向 google 发出的请求的 ip。 This works fine.. sometimes.这很好用..有时。 For some reason after a few request pings my program starts slowing down and then I start getting 'Max Connection Exceeded' errors and as far as I can tell the ip's are fine.出于某种原因,在几次请求 ping 之后,我的程序开始变慢,然后我开始收到“超出最大连接数”错误,据我所知,ip 没问题。

I started thinking that google was blocking the ip but then I request to other website and the same error continues.. I started thinking that maybe the proxy server only allows a certain amount of requests in a certain time frame so I tried some free proxies but I get the same errors again.. Either way if I leave it alone for a while it seems to work fine and then a few dozen pings later and the errors start again.我开始认为谷歌阻止了 ip 但后来我请求其他网站并且同样的错误继续.. 我开始认为代理服务器可能只允许在特定时间范围内的一定数量的请求所以我尝试了一些免费代理但是我再次遇到相同的错误。无论哪种方式,如果我将其单独放置一段时间,它似乎工作正常,然后在几十个 ping 之后,错误又开始了。

The only think I can think of and not sure how I can test or even if it's possible to test remotely (work router) is that maybe some open connections remain open and affects my code and then when the backlog of open connections are thrown out I can resume as normal.我唯一能想到但不确定如何测试或者即使可以远程测试(工作路由器)的想法是,也许一些打开的连接仍然打开并影响我的代码,然后当打开连接的积压被抛出时,我可以恢复正常。 I started looking for if I needed to "close" my connection after my get request but it doesn't seem necessary (although I wasn't even sure what I was looking for).我开始寻找在获取请求后是否需要“关闭”我的连接,但这似乎没有必要(尽管我什至不确定我在寻找什么)。

This is one version of my code, I've tried without sessions, I've tried a different way of writing the sessions.这是我的代码的一个版本,我尝试过不使用会话,我尝试过另一种编写会话的方式。 All seem to work in the same way:一切似乎都以相同的方式工作:

import requests, random

username = 'xxx'
password = 'yyy'

with open('proxies.txt', 'r') as p:
    proxy_lost = p.read().splitlines()
with open('data.txt', 'r') as d:
    data = d.read().splitlines()

for i in data:
    result = None
    while result is None:
        try:
            proxy = proxy_list[random.randint(0,len(proxy_list)-1)] + '.nordvpn.com'
            prox = 'socks5://{}:{}@{}:{}'.format(username, password, proxy, 1080) #I've also tried socks5h

            with requests.Session() as r:
                r.proxies['http': prox]
                r.proxies['https': prox]

                result = r.get('http://icanhazip.com')
                print(result.text.strip())

If anyone has any idea whatsoever any help is appreciated.如果有人有任何想法,任何帮助表示赞赏。 I've reached a point where i'm struggling to think of new ideas to try.我已经到了一个地步,我正在努力想出新的想法来尝试。

This is an example of one of the errors I get during this whole process:这是我在整个过程中遇到的错误之一的示例:

Traceback (most recent call last): File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py", line 809, in connect negotiate(self, dest_addr, dest_port) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py", line 444, in _negotiate_SOCKS5 self, CONNECT, dest_addr) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py", line 503, in _SOCKS5_request raise SOCKS5AuthError("SOCKS5 authentication failed") socks.SOCKS5AuthError: SOCKS5 authentication failed Traceback(最近一次调用最后一次):文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py”,第 809 行,在连接协商(self,dest_addr,dest_port)中文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py”,第 444 行,在 _negotiate_SOCKS5 self, CONNECT, dest_addr) 文件“C:\Users*\AppData\ Local\Programs\Python\Python37\lib\site-packages\socks.py",第 503 行,在 _SOCKS5_request 中引发 SOCKS5AuthError("SOCKS5 authentication failed") socks.SOCKS5AuthError: SOCKS5 authentication failed

During handling of the above exception, another exception occurred:在处理上述异常的过程中,又出现了一个异常:

Traceback (most recent call last): File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\contrib\socks.py", line 88, in _new_conn **extra_kw File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py", line 209, in create_connection raise err File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py", line 199, in create_connection sock.connect((remote_host, remote_port)) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py", line 47, in wrapper return function(*args, **kwargs) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py", line 814, in connect raise GeneralProxyError("Socket error", error) socks.GeneralProxyError: Socket error: SOCKS5 authentication failed Traceback(最近一次调用最后一次):文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\contrib\socks.py”,第 88 行,在 _new_conn **extra_kw 文件中“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py”,第 209 行,在 create_connection 引发错误文件“C:\Users*\AppData\Local\Programs\Python \Python37\lib\site-packages\socks.py”,第 199 行,在 create_connection sock.connect((remote_host, remote_port)) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site -packages\socks.py”,第 47 行,在包装返回函数(*args,**kwargs)文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\socks.py ",第 814 行,在连接中引发 GeneralProxyError("Socket error", error) socks.GeneralProxyError: Socket error: SOCKS5 authentication failed

During handling of the above exception, another exception occurred:在处理上述异常的过程中,又出现了一个异常:

Traceback (most recent call last): File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen chunked=chunked) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 354, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1244, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1290, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1239, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 1026, in _send_output sel回溯(最后一次调用):文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py”,第 600 行,在 urlopen chunked=chunked) 文件中C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py”,第 354 行,在 _make_request conn.request(方法,url,**httplib_request_kw)文件“C: \Users*\AppData\Local\Programs\Python\Python37\lib\http\client.py”,第 1244 行,在请求中 self._send_request(方法,url,正文,标头,encode_chunked)文件“C:\Users*\ AppData\Local\Programs\Python\Python37\lib\http\client.py",第 1290 行,在 _send_request self.endheaders(body, encode_chunked=encode_chunked) 文件“C:\Users*\AppData\Local\Programs\Python\ Python37\lib\http\client.py”,第 1239 行,在 endheaders self._send_output(message_body, encode_chunked=encode_chunked) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\http\client. py",第 1026 行,在 _send_output sel 中f.send(msg) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 966, in send self.connect() File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connection.py", line 181, in connect conn = self._new_conn() File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\contrib\socks.py", line 110, in _new_conn "Failed to establish a new connection: %s" % error urllib3.exceptions.NewConnectionError: : Failed to establish a new connection: SOCKS5 authentication failed f.send(msg) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\http\client.py”,第 966 行,发送 self.connect() 文件“C:\Users* \AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connection.py",第 181 行,在连接 conn = self._new_conn() 文件“C:\Users*\AppData\Local\Programs\ Python\Python37\lib\site-packages\urllib3\contrib\socks.py”,第 110 行,在 _new_conn “无法建立新连接:%s”% 错误 urllib3.exceptions.NewConnectionError: : 无法建立新连接: SOCKS5 认证失败

During handling of the above exception, another exception occurred:在处理上述异常的过程中,又出现了一个异常:

Traceback (most recent call last): File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py", line 449, in send timeout=timeout File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen _stacktrace=sys.exc_info()[2]) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\retry.py", line 399, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: SOCKSHTTPConnectionPool(host='icanhazip.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: SOCKS5 authentication failed')) Traceback(最近一次调用最后一次):文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py”,第 449 行,发送超时=超时文件“C :\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py”,第 638 行,在 urlopen _stacktrace=sys.exc_info()[2]) 文件“C:\Users *\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\retry.py”,第 399 行,增量引发 MaxRetryError(_pool,url,错误或 ResponseError(原因))urllib3.exceptions。 MaxRetryError: SOCKSHTTPConnectionPool(host='icanhazip.com', port=80): 超过 url 的最大重试次数:/(由 NewConnectionError 引起(':无法建立新连接:SOCKS5 身份验证失败'))

During handling of the above exception, another exception occurred:在处理上述异常的过程中,又出现了一个异常:

Traceback (most recent call last): File "C:\Users*\Documents_Scripts\Find Proxies\FindProxies.py", line 23, in result = requests.get(' http://icanhazip.com ', proxies=proxies) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 75, in get return request('get', url, params=params, **kwargs) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "C:\Users*\AppData\Local\Programs\P回溯(最近一次调用最后):文件“C:\Users*\Documents_Scripts\Find Proxies\FindProxies.py”,第 23 行,结果 = requests.get(' http://icanhazip.com ', proxies=proxies)文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py”,第 75 行,在获取返回请求('get',url,params=params,* *kwargs) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py”,第 60 行,在请求中返回 session.request(method=method, url= url, **kwargs) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py”,第 533 行,在请求中 resp = self.send(prep, **send_kwargs) 文件“C:\Users*\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py”,第 646 行,发送 r = adapter.send(request, ** kwargs) 文件 "C:\Users*\AppData\Local\Programs\P ython\Python37\lib\site-packages\requests\adapters.py", line 516, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: SOCKSHTTPConnectionPool(host='icanhazip.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: SOCKS5 authentication failed')) ython\Python37\lib\site-packages\requests\adapters.py",第 516 行,在发送中引发 ConnectionError(e, request=request) requests.exceptions.ConnectionError: SOCKSHTTPConnectionPool(host='icanhazip.com', port=80 ): url 超过最大重试次数: / (由 NewConnectionError 引起(': 无法建立新连接: SOCKS5 身份验证失败'))

Are you also closing the sessions again after use?您是否也在使用后再次关闭会话?

Try with:尝试:

r = requests.session(config={'keep_alive': False})

Inspired by: python-requests-close-http-connection灵感来源: python-requests-close-http-connection

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

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