Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

selenium proxy with authentication

import os
import zipfile

from selenium import webdriver

def initiate(proxy):
    proxy = proxy.replace("http://", "").replace("https://", "")
    PROXY_HOST = proxy.split("@")[1].split(":")[0]          #host
    PROXY_PORT = int(proxy.split("@")[1].split(":")[1])     #port
    PROXY_USER = proxy.split("@")[0].split(":")[0]          #username
    PROXY_PASS = proxy.split("@")[0].split(":")[1]          #password

    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']
    );
    """ % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)

    path = os.path.dirname(os.path.abspath(__file__))
    chrome_options = webdriver.ChromeOptions()
    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)
        os.path.join(path, 'chromedriver'),
        chrome_options=chrome_options)
    return driver

def main():
    driver = initiate("username:password@host:port")
    #driver.get('https://www.google.com/search?q=my+ip+address')
    driver.get('https://api.ipify.org/?format=json')

if __name__ == '__main__':
    main()
Comment

selenium proxy with authentication

import os
import zipfile

from selenium import webdriver

def initiate(proxy):
    proxy = proxy.replace("http://", "").replace("https://", "")
    PROXY_HOST = proxy.split("@")[1].split(":")[0]          #host
    PROXY_PORT = int(proxy.split("@")[1].split(":")[1])     #port
    PROXY_USER = proxy.split("@")[0].split(":")[0]          #username
    PROXY_PASS = proxy.split("@")[0].split(":")[1]          #password

    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']
    );
    """ % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)

    path = os.path.dirname(os.path.abspath(__file__))
    chrome_options = webdriver.ChromeOptions()
    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)
        os.path.join(path, 'chromedriver'),
        chrome_options=chrome_options)
    return driver

def main():
    driver = initiate("username:password@host:port")
    #driver.get('https://www.google.com/search?q=my+ip+address')
    driver.get('https://api.ipify.org/?format=json')

if __name__ == '__main__':
    main()
Comment

PREVIOUS NEXT
Code Example
Python :: python string lowercase 
Python :: python switch case 
Python :: how to print list without newline 
Python :: legend ax matplotlib 
Python :: pd merge_asof 
Python :: python not showing in control panel but showing not installed 
Python :: unpersist cache pyspark 
Python :: how to remove groups/user_permissions from user admin panel in django,how to edit fields shown on user admin panel 
Python :: comment transformer un chaine de caractere en liste python 
Python :: install python modules without pip 
Python :: how to make input box if else statement in tkinter 
Python :: how to install qrcode module in python 
Python :: remove grid in imshow 
Python :: how to check mix types in pandas column 
Python :: split custom pytorch dataset 
Python :: python trace code execution 
Python :: how to add to beginning of linked list python 
Python :: pip ne marche pas 
Python :: change state enabled tkinter 
Python :: how to get the memory location of a varible in python 
Python :: The function to be built, amino_acids, must return a list of a tuple and an integer when given a string of mRNA code. 
Python :: how to python string up 
Python :: ord() python 
Python :: sns.savefig 
Python :: dbscan example 
Python :: django pass list of fields to values 
Python :: combinations 
Python :: convert file dta in csv 
Python :: dates and times in python 
Python :: Python use number twice without variable 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =