Backconnect-Proxy, Zeitüberschreitung

Sockets, TCP/IP, (XML-)RPC und ähnliche Themen gehören in dieses Forum
Antworten
3frenky3
User
Beiträge: 2
Registriert: Freitag 14. Juli 2023, 16:09

Guten Tag. Das Problem ist nicht sehr kompliziert, aber ich kann keine Lösung finden.

Code: Alles auswählen

def main_request(data, proxy='', type_of_proxy='socks5', user_agent='Mozilla/5.0 (Linux; arm; Android 13; PEPM00) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.141 YaBrowser/22.3.6.61.00 SA/3 Mobile Safari/537.36'):
 
s = requests.Session()
 
 
 
retry = Retry(connect=3, backoff_factor=0.7)
adapter = HTTPAdapter(max_retries=retry)
s.mount('http://', adapter)
s.mount('https://', adapter)
 
if type_of_proxy == 'socks5':
    timeout_request = 3
elif type_of_proxy == 'http':
    timeout_request = 1
 
proxies = {
                'http': f'{type_of_proxy}://{proxy}',
                'https': f'{type_of_proxy}://{proxy}'
            }
 
page_login = s.post('https://my-site.com', data=data_1.encode('utf-8'), verify=False,
                                proxies=proxies, headers=headers_dic,timeout=timeout_request)
 
print(main_request(data='1',proxy='login:pass@backconnect.proxy.com:16000',type_of_proxy='http'))
Das Problem ist, dass ich bei einer Anfrage 10 bis 120 Sekunden auf eine Antwort warte. Manchmal kommt es in den ersten 3 Sekunden.
Wie kann ich das Warten auf eine Antwort begrenzen? Ich habe schon alles versucht.

Bei sock5 funktioniert alles wie es soll.
3frenky3
User
Beiträge: 2
Registriert: Freitag 14. Juli 2023, 16:09

Das Problem ist gelöst! Hier ist der Arbeitscode, falls ihn jemand braucht

Code: Alles auswählen

def main_request(data, proxy='', type_of_proxy='socks5', user_agent='Mozilla/5.0 (Linux; arm; Android 13; PEPM00) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.141 YaBrowser/22.3.6.61.00 SA/3 Mobile Safari/537.36'):
 
class TimeoutHTTPAdapter(HTTPAdapter):
    def __init__(self, *args, **kwargs):
        if "timeout" in kwargs:
            self.timeout = kwargs["timeout"]
            del kwargs["timeout"]
        super().__init__(*args, **kwargs)
 
    def send(self, request, **kwargs):
        timeout = kwargs.get("timeout")
        if timeout is None and hasattr(self, 'timeout'):
            kwargs["timeout"] = self.timeout
        return super().send(request, **kwargs)
  
s = requests.Session()
  
 s.mount('http://', TimeoutHTTPAdapter(timeout=5))  # 5 seconds
s.mount('https://', TimeoutHTTPAdapter(timeout=5))
  
  
proxies = {
                'http': f'{type_of_proxy}://{proxy}',
                'https': f'{type_of_proxy}://{proxy}'
            }
  
page_login = s.post('https://my-site.com', data=data_1.encode('utf-8'), verify=False,
                                proxies=proxies, headers=headers_dic,timeout=timeout_request)
  
print(main_request(data='1',proxy='login:pass@backconnect.proxy.com:16000',type_of_proxy='http'))
Antworten