Created
April 15, 2019 02:36
-
-
Save BobDu/681707cf8649e0b4772f16cb896634af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! usr/bin/env python3 | |
# pip install --force-reinstall https://github.com/Bob-Du/selenium-wire/archive/header_overrides.zip | |
import time | |
import hashlib | |
import toml | |
from seleniumwire import webdriver | |
cfg = toml.load('test.toml') | |
app_key = cfg.get('app-key') | |
app_secret = cfg.get('app-secret') | |
proxy_ip = cfg.get('proxy-ip') | |
proxy_port = cfg.get('proxy-port') | |
def get_proxy_auth_header(start_transaction=None, hold_transaction=None): | |
parmas = { | |
'app_key': app_key, | |
'timestamp': time.strftime(r'%Y-%m-%d %H:%M:%S'), | |
} | |
if start_transaction: | |
print('start_transaction') | |
parmas['release-transaction'] = '1' | |
parmas['with-transaction'] = '1' | |
if hold_transaction: | |
print('hold_transaction') | |
parmas['with-transaction'] = '1' | |
codes = app_secret | |
for k, v in sorted(parmas.items(), key=lambda x: x[0]): | |
codes += k + v | |
codes += app_secret | |
print(codes) | |
sign = hashlib.md5(codes.encode('utf-8')).hexdigest().upper() | |
proxy_auth_header = 'MYH-AUTH-MD5 sign=' + sign | |
for k, v in parmas.items(): | |
proxy_auth_header += '&' + k + '=' + v | |
return proxy_auth_header | |
chrome_options = webdriver.ChromeOptions() | |
chrome_options.add_argument('--no-sandbox') | |
# chrome_options.add_argument('--headless') | |
chrome_options.add_argument('--disable-gpu') | |
prefs = { | |
'profile.default_content_setting_values': { | |
'images': 2, | |
'javascript': 2 | |
} | |
} | |
chrome_options.add_experimental_option('prefs', prefs) | |
seleniumwire_options = { | |
'proxy': { | |
'http': 'https://' + proxy_ip + ':' + proxy_port, | |
'https': 'https://' + proxy_ip + ':' + proxy_port, | |
'no_proxy': 'localhost,127.0.0.1' | |
}, | |
'ssl_verify': False, | |
'disable_encoding': True | |
} | |
driver = webdriver.Chrome(chrome_options=chrome_options, seleniumwire_options=seleniumwire_options) | |
driver.header_overrides = { | |
'X-Proxy-Authorization': get_proxy_auth_header(start_transaction=True) | |
} | |
driver.get('https://httpbin.org/get') | |
# driver.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment