From 8b36a8916275050e741cdc8bd80c9b500e371d9a Mon Sep 17 00:00:00 2001 From: Germey Date: Mon, 10 Jul 2017 21:29:04 +0800 Subject: [PATCH] first commit --- .gitignore | 1 + reqeusts_socks_way2.py | 12 +++++++ requests_auth.py | 13 +++++++ requests_no_auth.py | 13 +++++++ requests_socks.py | 12 +++++++ selenium_chrome_auth.py | 68 +++++++++++++++++++++++++++++++++++ selenium_chrome_no_auth.py | 7 ++++ selenium_phantomjs_auth.py | 10 ++++++ selenium_phantomjs_no_auth.py | 10 ++++++ urllib_auth.py | 15 ++++++++ urllib_no_auth.py | 15 ++++++++ urllib_socks.py | 15 ++++++++ 12 files changed, 191 insertions(+) create mode 100644 .gitignore create mode 100644 reqeusts_socks_way2.py create mode 100644 requests_auth.py create mode 100644 requests_no_auth.py create mode 100644 requests_socks.py create mode 100644 selenium_chrome_auth.py create mode 100644 selenium_chrome_no_auth.py create mode 100644 selenium_phantomjs_auth.py create mode 100644 selenium_phantomjs_no_auth.py create mode 100644 urllib_auth.py create mode 100644 urllib_no_auth.py create mode 100644 urllib_socks.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..757fee3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea \ No newline at end of file diff --git a/reqeusts_socks_way2.py b/reqeusts_socks_way2.py new file mode 100644 index 0000000..5bd368b --- /dev/null +++ b/reqeusts_socks_way2.py @@ -0,0 +1,12 @@ +# pip3 install PySocks +import requests +import socks +import socket + +socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 9742) +socket.socket = socks.socksocket +try: + response = requests.get('http://httpbin.org/get') + print(response.text) +except requests.exceptions.ConnectionError as e: + print('Error', e.args) diff --git a/requests_auth.py b/requests_auth.py new file mode 100644 index 0000000..e9162c2 --- /dev/null +++ b/requests_auth.py @@ -0,0 +1,13 @@ +import requests + +proxy = 'username:password@127.0.0.1:9743' + +proxies = { + 'http': 'http://' + proxy, + 'https': 'https://' + proxy, +} +try: + response = requests.get('http://httpbin.org/get', proxies=proxies) + print(response.text) +except requests.exceptions.ConnectionError as e: + print('Error', e.args) diff --git a/requests_no_auth.py b/requests_no_auth.py new file mode 100644 index 0000000..0c1f0be --- /dev/null +++ b/requests_no_auth.py @@ -0,0 +1,13 @@ +import requests + +proxy = '127.0.0.1:9743' + +proxies = { + 'http': 'http://' + proxy, + 'https': 'https://' + proxy, +} +try: + response = requests.get('http://httpbin.org/get', proxies=proxies) + print(response.text) +except requests.exceptions.ConnectionError as e: + print('Error', e.args) diff --git a/requests_socks.py b/requests_socks.py new file mode 100644 index 0000000..954450f --- /dev/null +++ b/requests_socks.py @@ -0,0 +1,12 @@ +import requests + +proxy = '127.0.0.1:9742' +proxies = { + 'http': 'socks5://' + proxy, + 'https': 'socks5://' + proxy +} +try: + response = requests.get('http://httpbin.org/get', proxies=proxies) + print(response.text) +except requests.exceptions.ConnectionError as e: + print('Error', e.args) diff --git a/selenium_chrome_auth.py b/selenium_chrome_auth.py new file mode 100644 index 0000000..bafea4f --- /dev/null +++ b/selenium_chrome_auth.py @@ -0,0 +1,68 @@ +from selenium import webdriver +from selenium.webdriver.chrome.options import Options +import zipfile + +ip = '127.0.0.1' +port = 9743 +username = 'foo' +password = 'bar' + +manifest_json = """ +{ + "version": "1.0.0", + "manifest_version": 2, + "name": "Chrome Proxy", + "permissions": [ + "proxy", + "tabs", + "unlimitedStorage", + "storage", + "", + "webRequest", + "webRequestBlocking" + ], + "background": { + "scripts": ["background.js"] + } +} +""" + +background_js = """ +var config = { + mode: "fixed_servers", + rules: { + singleProxy: { + scheme: "http", + host: "%(ip)s", + port: %(port)s + } + } + } + +chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); + +function callbackFn(details) { + return { + authCredentials: { + username: "%(username)s", + password: "%(password)s" + } + } +} + +chrome.webRequest.onAuthRequired.addListener( + callbackFn, + {urls: [""]}, + ['blocking'] +) +""" % {'ip': ip, 'port': port, 'username': username, 'password': password} + +plugin_file = 'proxy_auth_plugin.zip' +with zipfile.ZipFile(plugin_file, 'w') as zp: + zp.writestr("manifest.json", manifest_json) + zp.writestr("background.js", background_js) +chrome_options = Options() +chrome_options.add_argument("--start-maximized") +chrome_options.add_extension(plugin_file) +browser = webdriver.Chrome(chrome_options=chrome_options) +browser.get('http://httpbin.org/get') diff --git a/selenium_chrome_no_auth.py b/selenium_chrome_no_auth.py new file mode 100644 index 0000000..1506e9a --- /dev/null +++ b/selenium_chrome_no_auth.py @@ -0,0 +1,7 @@ +from selenium import webdriver + +proxy = '127.0.0.1:9743' +chrome_options = webdriver.ChromeOptions() +chrome_options.add_argument('--proxy-server=http://' + proxy) +chrome = webdriver.Chrome(chrome_options=chrome_options) +chrome.get('http://httpbin.org/get') diff --git a/selenium_phantomjs_auth.py b/selenium_phantomjs_auth.py new file mode 100644 index 0000000..6d05450 --- /dev/null +++ b/selenium_phantomjs_auth.py @@ -0,0 +1,10 @@ +from selenium import webdriver + +service_args = [ + '--proxy=127.0.0.1:9743', + '--proxy-type=http' +] + +browser = webdriver.PhantomJS(service_args=service_args) +browser.get('http://httpbin.org/get') +print(browser.page_source) diff --git a/selenium_phantomjs_no_auth.py b/selenium_phantomjs_no_auth.py new file mode 100644 index 0000000..6d05450 --- /dev/null +++ b/selenium_phantomjs_no_auth.py @@ -0,0 +1,10 @@ +from selenium import webdriver + +service_args = [ + '--proxy=127.0.0.1:9743', + '--proxy-type=http' +] + +browser = webdriver.PhantomJS(service_args=service_args) +browser.get('http://httpbin.org/get') +print(browser.page_source) diff --git a/urllib_auth.py b/urllib_auth.py new file mode 100644 index 0000000..5adf928 --- /dev/null +++ b/urllib_auth.py @@ -0,0 +1,15 @@ +from urllib.error import URLError +from urllib.request import ProxyHandler, build_opener + +proxy = 'username:password@127.0.0.1:9743' + +proxy_handler = ProxyHandler({ + 'http': 'http://' + proxy, + 'https': 'https://' + proxy +}) +opener = build_opener(proxy_handler) +try: + response = opener.open('http://httpbin.org/get') + print(response.read().decode('utf-8')) +except URLError as e: + print(e.reason) diff --git a/urllib_no_auth.py b/urllib_no_auth.py new file mode 100644 index 0000000..ec4dded --- /dev/null +++ b/urllib_no_auth.py @@ -0,0 +1,15 @@ +from urllib.error import URLError +from urllib.request import ProxyHandler, build_opener + +proxy = '127.0.0.1:9743' + +proxy_handler = ProxyHandler({ + 'http': 'http://' + proxy, + 'https': 'https://' + proxy +}) +opener = build_opener(proxy_handler) +try: + response = opener.open('http://httpbin.org/get') + print(response.read().decode('utf-8')) +except URLError as e: + print(e.reason) diff --git a/urllib_socks.py b/urllib_socks.py new file mode 100644 index 0000000..02b395a --- /dev/null +++ b/urllib_socks.py @@ -0,0 +1,15 @@ + +# pip3 install PySocks + +import socks +import socket +from urllib import request +from urllib.error import URLError + +socks.set_default_proxy(socks.SOCKS5, '127.0.0.1', 9742) +socket.socket = socks.socksocket +try: + response = request.urlopen('http://httpbin.org/get') + print(response.read().decode('utf-8')) +except URLError as e: + print(e.reason)