旋转代理不会在发送的请求中使用,但会旋转

Rotating proxies don't get used in requests sent, but do rotate

我试图在 x 次请求后更改代理以根据 api 检查用户名可用性。但是,我更改代理的功能似乎并没有更改我发送的请求中的代理。在 proxy 函数中,代理确实发生了变化。谁能弄清楚为什么代理在发送的请求中不起作用?

(旧)代码:

proxy = ''
proxyFile = open(f'external/proxies.txt', 'r')
proxyList = [line.split(',') for line in proxyFile.readlines()]
...
    def proxies(self):
        try: 
            if self.proxyCount > 0:
                self.proxyCount += 1
            proxy = random.choice(self.proxyList)
            print('proxy in proxies function: ', proxy)
            return proxy
        except:
            pass

   def checkAccounts(self):
        while not self.usernames.empty():
            name = self.usernames.get(); self.usernames.put(name)
            url = f"https://public-ubiservices.ubi.com/v3/profiles?nameOnPlatform={name}&platformType=uplay"
            try:         
                r = requests.get(url, headers=self.headers, proxies=self.proxy)
                print('proxy in check function: ', self.proxy)
                
                try:
                    if self.checkedCount % 100 == 0:
                        self.proxies()
                except:
                    pass

                ctypes.windll.kernel32.SetConsoleTitleW(f"Gx | Checked: {self.checkedCount}, Available: {self.availableCount}, Errors: {self.errorCount}")
                if r.status_code == 200:
                    self.checkedCount += 1
                    if len(r.json()['profiles']) != 0:
                        print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Taken:          {name}")       
                    
                    else:  
                        print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Available:      {name}")
                        self.availableCount += 1

                if r.status_code == 429:
                    self.errorCount += 1
                    print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Error:          Rate limited")
                    self.proxies()
            
            except Exception:
                pass

输出:

[+] Taken:      name
proxy in proxies function:  ['222.74.202.234:80\n']
proxy in check function:

Previous post about the same topic


编辑 1:

    with open('external/proxies.txt', 'r') as f:
        proxyList = [line.strip() for line in f]
...

    def proxies(self):
        if self.proxyCount > 0:
            self.proxyCount += 1
        proxy = random.choice(self.proxyList)
        print(proxy)
        return proxy

Edit1 的输出:

85.214.65.246:80
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\Gibbo\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Users\Gibbo\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Gibbo\OneDrive\Bureaublad\[+] Projects\Main\[+] Ubi checker\ubichecker.py", line 66, in checkAccounts
    r = requests.get(url, headers=self.headers, proxies=self.proxy)
  File "C:\Users\Gibbo\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\Gibbo\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\Gibbo\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 532, in request
    settings = self.merge_environment_settings(
  File "C:\Users\Gibbo\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 710, in merge_environment_settings
    no_proxy = proxies.get('no_proxy') if proxies is not None else None
AttributeError: 'str' object has no attribute 'get'

您没有将代理更改功能的 return 值分配给 self.proxy。修复应该使代理更改


proxyList = [proxyIp.strip() for line in proxyFile.readlines() for proxyIp in line.split(',') if proxyIp]

def proxies(self):
    try: 
        if self.proxyCount > 0:
            self.proxyCount += 1
        proxy = random.choice(self.proxyList)
        print('proxy in proxies function: ', proxy)
        return {
            'http': f'http://{proxy}',
            'https': f'http://{proxy}',
        }
    except:
        pass

def checkAccounts(self):
    # set the proxy initially, if not set already
    self.proxy = self.proxies()
    while not self.usernames.empty():
        name = self.usernames.get(); self.usernames.put(name)
        url = f"https://public-ubiservices.ubi.com/v3/profiles?nameOnPlatform={name}&platformType=uplay"
        try:         
            r = requests.get(url, headers=self.headers, proxies=self.proxy)
            print('proxy in check function: ', self.proxy)
            
            try:
                if self.checkedCount % 100 == 0:
                    self.proxy = self.proxies()
            except:
                pass

            ctypes.windll.kernel32.SetConsoleTitleW(f"Gx | Checked: {self.checkedCount}, Available: {self.availableCount}, Errors: {self.errorCount}")
            if r.status_code == 200:
                self.checkedCount += 1
                if len(r.json()['profiles']) != 0:
                    print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Taken:          {name}")       
                
                else:  
                    print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Available:      {name}")
                    self.availableCount += 1

            if r.status_code == 429:
                self.errorCount += 1
                print(f"{Fore.LIGHTBLACK_EX}[+]{Fore.RESET} Error:          Rate limited")
                self.proxy = self.proxies()
        
        except Exception:
            pass

编辑:看来您的 proxyList 需要先展平。