├── .gitignore ├── README.md ├── reqeusts_socks_way2.py ├── requests_auth.py ├── requests_no_auth.py ├── requests_socks.py ├── selenium_chrome_auth.py ├── selenium_chrome_no_auth.py ├── selenium_phantomjs_auth.py ├── selenium_phantomjs_no_auth.py ├── urllib_auth.py ├── urllib_no_auth.py └── urllib_socks.py /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ProxySettings 2 | Proxy Settings 3 | -------------------------------------------------------------------------------- /reqeusts_socks_way2.py: -------------------------------------------------------------------------------- 1 | # pip3 install PySocks 2 | import requests 3 | import socks 4 | import socket 5 | 6 | socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 9742) 7 | socket.socket = socks.socksocket 8 | try: 9 | response = requests.get('http://httpbin.org/get') 10 | print(response.text) 11 | except requests.exceptions.ConnectionError as e: 12 | print('Error', e.args) 13 | -------------------------------------------------------------------------------- /requests_auth.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | proxy = 'username:password@127.0.0.1:9743' 4 | 5 | proxies = { 6 | 'http': 'http://' + proxy, 7 | 'https': 'https://' + proxy, 8 | } 9 | try: 10 | response = requests.get('http://httpbin.org/get', proxies=proxies) 11 | print(response.text) 12 | except requests.exceptions.ConnectionError as e: 13 | print('Error', e.args) 14 | -------------------------------------------------------------------------------- /requests_no_auth.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | proxy = '127.0.0.1:9743' 4 | 5 | proxies = { 6 | 'http': 'http://' + proxy, 7 | 'https': 'https://' + proxy, 8 | } 9 | try: 10 | response = requests.get('http://httpbin.org/get', proxies=proxies) 11 | print(response.text) 12 | except requests.exceptions.ConnectionError as e: 13 | print('Error', e.args) 14 | -------------------------------------------------------------------------------- /requests_socks.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | proxy = '127.0.0.1:9742' 4 | proxies = { 5 | 'http': 'socks5://' + proxy, 6 | 'https': 'socks5://' + proxy 7 | } 8 | try: 9 | response = requests.get('http://httpbin.org/get', proxies=proxies) 10 | print(response.text) 11 | except requests.exceptions.ConnectionError as e: 12 | print('Error', e.args) 13 | -------------------------------------------------------------------------------- /selenium_chrome_auth.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | from selenium.webdriver.chrome.options import Options 3 | import zipfile 4 | 5 | ip = '127.0.0.1' 6 | port = 9743 7 | username = 'foo' 8 | password = 'bar' 9 | 10 | manifest_json = """ 11 | { 12 | "version": "1.0.0", 13 | "manifest_version": 2, 14 | "name": "Chrome Proxy", 15 | "permissions": [ 16 | "proxy", 17 | "tabs", 18 | "unlimitedStorage", 19 | "storage", 20 | "", 21 | "webRequest", 22 | "webRequestBlocking" 23 | ], 24 | "background": { 25 | "scripts": ["background.js"] 26 | } 27 | } 28 | """ 29 | 30 | background_js = """ 31 | var config = { 32 | mode: "fixed_servers", 33 | rules: { 34 | singleProxy: { 35 | scheme: "http", 36 | host: "%(ip)s", 37 | port: %(port)s 38 | } 39 | } 40 | } 41 | 42 | chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); 43 | 44 | function callbackFn(details) { 45 | return { 46 | authCredentials: { 47 | username: "%(username)s", 48 | password: "%(password)s" 49 | } 50 | } 51 | } 52 | 53 | chrome.webRequest.onAuthRequired.addListener( 54 | callbackFn, 55 | {urls: [""]}, 56 | ['blocking'] 57 | ) 58 | """ % {'ip': ip, 'port': port, 'username': username, 'password': password} 59 | 60 | plugin_file = 'proxy_auth_plugin.zip' 61 | with zipfile.ZipFile(plugin_file, 'w') as zp: 62 | zp.writestr("manifest.json", manifest_json) 63 | zp.writestr("background.js", background_js) 64 | chrome_options = Options() 65 | chrome_options.add_argument("--start-maximized") 66 | chrome_options.add_extension(plugin_file) 67 | browser = webdriver.Chrome(chrome_options=chrome_options) 68 | browser.get('http://httpbin.org/get') 69 | -------------------------------------------------------------------------------- /selenium_chrome_no_auth.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | 3 | proxy = '127.0.0.1:9743' 4 | chrome_options = webdriver.ChromeOptions() 5 | chrome_options.add_argument('--proxy-server=http://' + proxy) 6 | chrome = webdriver.Chrome(chrome_options=chrome_options) 7 | chrome.get('http://httpbin.org/get') 8 | -------------------------------------------------------------------------------- /selenium_phantomjs_auth.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | 3 | service_args = [ 4 | '--proxy=127.0.0.1:9743', 5 | '--proxy-type=http', 6 | '--proxy-auth=username:password' 7 | ] 8 | 9 | browser = webdriver.PhantomJS(service_args=service_args) 10 | browser.get('http://httpbin.org/get') 11 | print(browser.page_source) 12 | -------------------------------------------------------------------------------- /selenium_phantomjs_no_auth.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | 3 | service_args = [ 4 | '--proxy=127.0.0.1:9743', 5 | '--proxy-type=http' 6 | ] 7 | 8 | browser = webdriver.PhantomJS(service_args=service_args) 9 | browser.get('http://httpbin.org/get') 10 | print(browser.page_source) 11 | -------------------------------------------------------------------------------- /urllib_auth.py: -------------------------------------------------------------------------------- 1 | from urllib.error import URLError 2 | from urllib.request import ProxyHandler, build_opener 3 | 4 | proxy = 'username:password@127.0.0.1:9743' 5 | 6 | proxy_handler = ProxyHandler({ 7 | 'http': 'http://' + proxy, 8 | 'https': 'https://' + proxy 9 | }) 10 | opener = build_opener(proxy_handler) 11 | try: 12 | response = opener.open('http://httpbin.org/get') 13 | print(response.read().decode('utf-8')) 14 | except URLError as e: 15 | print(e.reason) 16 | -------------------------------------------------------------------------------- /urllib_no_auth.py: -------------------------------------------------------------------------------- 1 | from urllib.error import URLError 2 | from urllib.request import ProxyHandler, build_opener 3 | 4 | proxy = '127.0.0.1:9743' 5 | 6 | proxy_handler = ProxyHandler({ 7 | 'http': 'http://' + proxy, 8 | 'https': 'https://' + proxy 9 | }) 10 | opener = build_opener(proxy_handler) 11 | try: 12 | response = opener.open('http://httpbin.org/get') 13 | print(response.read().decode('utf-8')) 14 | except URLError as e: 15 | print(e.reason) 16 | -------------------------------------------------------------------------------- /urllib_socks.py: -------------------------------------------------------------------------------- 1 | 2 | # pip3 install PySocks 3 | 4 | import socks 5 | import socket 6 | from urllib import request 7 | from urllib.error import URLError 8 | 9 | socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 9742) 10 | socket.socket = socks.socksocket 11 | try: 12 | response = request.urlopen('http://httpbin.org/get') 13 | print(response.read().decode('utf-8')) 14 | except URLError as e: 15 | print(e.reason) 16 | --------------------------------------------------------------------------------