Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98979

How can i use variables from my python scripts inside of json data? [duplicate]

$
0
0

This question already has an answer here:

I am trying to access certian variables from my python code above of json data. I am new to using python and json together so im not sure how to go about this. here is my code:

global proxy_addressed
            ordered_proxies = []
            for numb in range(0, num_tasks):
                if numb % num_proxies == 0:
                    random.shuffle(add_proxy)
                proxy_addressed = add_proxy[numb % num_proxies]

                ordered_proxies.append(proxy_addressed)

                global pvar
                for lines in ordered_proxies:
                    pvar = lines.split(':')  # rotating proxy or host

                print(pvars)
                PROXY_HOST = pvar[0] + ':'
                PROXY_PORT = pvar[1] + ':'
                PROXY_USER = pvar[2] + ':'
                PROXY_PASS = pvar[3]

                proxy = PROXY_HOST + PROXY_PORT + PROXY_USER + PROXY_PASS
                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: PROXY_HOST,
                            port: PROXY_PORT
                          },
                          bypassList: ["foobar.com"]
                        }
                      };

                chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

                function callbackFn(details) {
                    return {
                        authCredentials: {
                            username: PROXY_USER,
                            password: PROXY_PASS
                        }
                    };
                }

                chrome.webRequest.onAuthRequired.addListener(
                            callbackFn,
                            {urls: ["<all_urls>"]},
                            ['blocking']
                );
                """

                pluginfile = 'proxy_auth_plugin.zip'

                with zipfile.ZipFile(pluginfile, 'w') as zp:
                    zp.writestr("manifest.json", manifest_json)
                    zp.writestr("background.js", background_js)

                co = Options()
                co.add_argument("--start-maximized")
                co.add_extension(pluginfile)
                co.add_argument(win_size)
                driver = webdriver.Chrome("chromedriver", chrome_options=co)
                driver.execute_script("window.open('{}')".format(page_address))

I am trying to use the variables PROXY_HOST, PROXY_PORT, PROXY_USER and PROXY_PASS inside of the manifest/background_json and it is unsuccessful. How can i call variables from the code into the json?

EDIT: I should be a bit more specific, i need to input the code into the js script that i wrote that then is being used by a json data. So really its like i need to be able to access a python variable in a js script.


Viewing all articles
Browse latest Browse all 98979

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>