0

when I put the proxy variable inside the for loop the code work fine, but when I print proxy outside the for loop it only give the first loop and stops, is there a way the proxy variable work fine if i used it outside the for loop.

value = ['3128', '3128', '3128', '3128']
num = ['40.69.144.143', '40.69.144.143', '40.69.144.143', '40.69.144.143']
for (a, c) in zip(num,value):
    proxy = a+':'+c
        print(proxy)

to explain what am doing, am doing a shoe bot that open multiple windows at the same time and each window has its own unique proxy, I want to put the proxy variable in the function get_chromedriver as a parameter.

from selenium import webdriver
import threading
import time
import zipfile

    proxy_host = ['40.122.24.111', '40.122.24.111', '40.122.24.111', '40.122.24.111']  # rotating proxy
    proxy_port = ['3128', '3128', '3128', '3128']
    proxy_username = ['lukmanaraby', 'lukmanaraby', 'lukmanaraby', 'lukmanaraby']
    proxy_password = ['aiobotagents', 'aiobotagents', 'aiobotagents', 'aiobotagents']
 for (a, c) in zip(proxy_host,proxy_port):
        proxy = a+':'+c
 
    
    def test_logic():
        url = 'https://whatismyipaddress.com/'
        webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe", options=chrome_options).get(url)
        # Implement your test logic
    def auth_json(host,port,user,password):
        global manifest_json
        global background_js
        manifest_json  = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Chrome Proxy",
        "permissions": [
            "proxy",
            "tabs",
            "unlimitedStorage",
            "storage",
            "<all_urls>",
            "webRequest",
            "webRequestBlocking"
        ],
        "background": {
            "scripts": ["background.js"]
        },
        "minimum_chrome_version":"22.0.0"
    }
    """
    
        background_js = """
    var config = {
            mode: "fixed_servers",
            rules: {
              singleProxy: {
                scheme: "http",
                host: "%s",
                port: parseInt(%s)
              },
              bypassList: ["localhost"]
            }
          };
    
    chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
    
    function callbackFn(details) {
        return {
            authCredentials: {
                username: "%s",
                password: "%s"
            }
        };
    }
    
    chrome.webRequest.onAuthRequired.addListener(
                callbackFn,
                {urls: ["<all_urls>"]},
                ['blocking']
    );
    """ % (host,port,user,password)
    
    
        #containing the authentication of the proxies.
    def proxy_auth(): #transfering the information of the proxies to auth_json function
     for (i,j,m,k) in zip(proxy_host, proxy_port, proxy_username, proxy_password):
        auth_json(i,j,m,k)
    def get_chromedriver(use_proxy=False, user_agent=proxy):
        global  chrome_options
        chrome_options = webdriver.ChromeOptions()
        if use_proxy:
            pluginfile = 'proxy_auth_plugin.zip'
    
            with zipfile.ZipFile(pluginfile, 'w') as zp:
                zp.writestr("manifest.json", manifest_json)
                zp.writestr("background.js", background_js)
            chrome_options.add_extension(pluginfile)
        if user_agent==proxy:
            chrome_options.add_argument('--proxy-server=%s' % user_agent)
        driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe",options=chrome_options)
        return driver
    
    N = 5   # Number of browsers to spawn
    thread_list = list()
    
    # Start test
    for i in proxy_host:
        get_chromedriver()
        proxy_auth()
        t = threading.Thread(name='Test {}'.format(i), target=test_logic)
        t.start()
        time.sleep(1)
        print (t.name + ' started!')
        thread_list.append(t)
    
    # Wait for all thre<ads to complete
    for thread in thread_list:
        thread.join()
    
    print ('Test completed!')
        enter code here
0

1 Answer 1

1

There's really no way I can think of to make it work while keeping variable type the same as it is right now (str). If assigned a value inside a for loop and accessed outside the loop it will only hold the last value it was assigned before the loop terminated.

The best way to do it is to create a list variable and append values to the list inside the loop. After that all values can be easily accessed from the resulting list:

value = ['3128', '3128', '3128', '3128']
num = ['40.69.144.143', '40.69.144.143', '40.69.144.143', '40.69.144.143']
proxy = []
for (a, c) in zip(num,value):
    proxy.append(a+':'+c)

for item in proxy:
    print(item)

Updated as per comments below: To use resulting variable in a function without assigning values to a list you can just put the function call inside the for loop.

def my_func(a):
   return a

value = ['3128', '3128', '3128', '3128']
num = ['40.69.144.143', '40.69.144.143', '40.69.144.143', '40.69.144.143']

for (a, c) in zip(num,value):
    proxy.append(a+':'+c)
    foo = my_func(proxy)

EDIT 2: So I had a look at your function and it seems like and easy fix to modify it to use lists instead of simple strings. This way the function will return a list of multiple drivers based on multiple proxies:

    def get_chromedriver(use_proxy=False, user_agent: list = proxy):
        global  chrome_options
        chrome_options = webdriver.ChromeOptions()
        if use_proxy:
            pluginfile = 'proxy_auth_plugin.zip'
    
            with zipfile.ZipFile(pluginfile, 'w') as zp:
                zp.writestr("manifest.json", manifest_json)
                zp.writestr("background.js", background_js)
            chrome_options.add_extension(pluginfile)
        drivers = []
        for i in user_agent:
            chrome_options.add_argument('--proxy-server=%s' % user_agent)
            drivers.append(webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe",options=chrome_options))
        return drivers
Sign up to request clarification or add additional context in comments.

5 Comments

sorry, but this solution don't help at all, what I want to do is to put the variable that was used in the for loop into a function, thank u for sharing <3
So why can't your function work with a list? Alternatively, put your function call inside the for loop.
am sorry am still a beginner in programming,but the function already in a for loop edit:I will post the whole code
@TahaDaboussi, I amended my answer with a different approach.
really appreciate the help, been stuck in this problem for multiple days thank u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.