本文整理汇总了Python中browsermobproxy.Server.create_proxy方法的典型用法代码示例。如果您正苦于以下问题:Python Server.create_proxy方法的具体用法?Python Server.create_proxy怎么用?Python Server.create_proxy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类browsermobproxy.Server
的用法示例。
在下文中一共展示了Server.create_proxy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def main(argv):
init()
parser = argparse.ArgumentParser()
parser.add_argument('-u', action='store', dest='start_url', help='Set page URL', required=True)
parser.add_argument('-c', action='store', dest='cookies_file', help='JSON file with cookies', required=False)
parser.add_argument('-w', action='store', dest='webdriver_type', help='Set WebDriver type (firefox or phantomjs, firebox by default)', default="firefox", required=False)
results = parser.parse_args()
start_url = results.start_url
cookies_file = results.cookies_file
webdriver_type = results.webdriver_type
allowed_domain = urlparse(start_url).netloc
browsermobproxy_path = get_browsermobproxy_path()
options = {
'port': 9090,
}
server = Server(browsermobproxy_path,options)
server.start()
proxy = server.create_proxy()
if webdriver_type == "phantomjs":
service_args = ['--proxy=localhost:9091','--proxy-type=http',]
driver = webdriver.PhantomJS(service_args=service_args)
driver.set_window_size(1440, 1024)
else:
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har('woodpycker', options={'captureHeaders': True, 'captureContent': True})
driver.get(start_url)
if not cookies_file is None:
with open(cookies_file, 'rb') as fp:
cookies = json.load(fp)
for cookie in cookies:
driver.add_cookie(cookie)
driver.refresh()
links = driver.find_elements_by_tag_name('a')
lenl = len(links)
for i in range(0,lenl):
if links[i].is_displayed():
url = links[i].get_attribute('href')
text = links[i].get_attribute('text')
if url.find(allowed_domain) != -1:
links[i].click()
print "%s Clicked on the link '%s' with HREF '%s'" % (Fore.BLUE+"*"+Fore.RESET,Style.BRIGHT+text+Style.RESET_ALL,Style.BRIGHT+url+Style.RESET_ALL)
show_status_codes(proxy.har,allowed_domain)
driver.back()
driver.refresh()
links = driver.find_elements_by_tag_name('a')
driver.quit()
server.stop()
示例2: save_web_page_stats_to_har
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def save_web_page_stats_to_har(url, webdriver_name, save_to_file):
"""Generate the HAR archive from an URL with the Selenium webdriver
'webdriver_name', saving the HAR file to 'save_to_file'
"""
browsermob_server = Server(Config.browsermob_executable)
browsermob_server.start()
random_port = get_a_random_free_tcp_port()
proxy_conn = browsermob_server.create_proxy({"port": random_port})
driver = create_selenium_webdriver(webdriver_name, proxy_conn)
try:
proxy_conn.new_har(url, options={'captureHeaders': True})
driver.get(url)
har_json = json.dumps(proxy_conn.har, ensure_ascii=False,
indent=4, separators=(',', ': '))
# Save '.HAR' file
with io.open(save_to_file + '.har', mode='wt', buffering=1,
encoding='utf8', errors='backslashreplace',
newline=None) as output_har_f:
output_har_f.write(unicode(har_json))
# Save '.PROF' file with profiling report (timings, sizes, etc)
with io.open(save_to_file + '.prof', mode='wb', buffering=1,
newline=None) as prof_output:
report_har_dictionary(proxy_conn.har, prof_output)
finally:
proxy_conn.close()
browsermob_server.stop()
driver.quit()
示例3: setupdevices
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def setupdevices():
"""
Description:
Sets u browser proxy, Selenium driver, and har object
Usage:
[driver,proxy]=setupdevices()
Inputs:
NA
Output:
Selenium driver
Browsermob proxy
Browsermob server
"""
#set up proxy
server = Server("############/browsermob-proxy-2.0-beta-9/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy()
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
proxy.new_har("________")
#set up driver
driver = webdriver.Firefox(firefox_profile=profile)
return (driver,proxy,server)
示例4: setUp
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def setUp(self):
"""
Start the browser with a browsermob-proxy instance for use by the test.
You *must* call this in the `setUp` method of any subclasses before using the browser!
Returns:
None
"""
try:
# Start server proxy
server = Server('browsermob-proxy')
server.start()
self.proxy = server.create_proxy()
proxy_host = os.environ.get('BROWSERMOB_PROXY_HOST', '127.0.0.1')
self.proxy.remap_hosts('localhost', proxy_host)
except:
self.skipTest('Skipping: could not start server with browsermob-proxy.')
# parent's setUp
super(WebAppPerfReport, self).setUp()
# Initialize vars
self._page_timings = []
self._active_har = False
self._with_cache = False
# Add one more cleanup for the server
self.addCleanup(server.stop)
示例5: CaptureNetworkTraffic
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def CaptureNetworkTraffic(url,server_ip,headers,file_path):
'''
This function can be used to capture network traffic from the browser. Using this function we can capture header/cookies/http calls made from the browser
url - Page url
server_ip - remap host to for specific URL
headers - this is a dictionary of the headers to be set
file_path - File in which HAR gets stored
'''
port = {'port':9090}
server = Server("G:\\browsermob\\bin\\browsermob-proxy",port) #Path to the BrowserMobProxy
server.start()
proxy = server.create_proxy()
proxy.remap_hosts("www.example.com",server_ip)
proxy.remap_hosts("www.example1.com",server_ip)
proxy.remap_hosts("www.example2.com",server_ip)
proxy.headers(headers)
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
new = {'captureHeaders':'True','captureContent':'True'}
proxy.new_har("google",new)
driver.get(url)
proxy.har # returns a HAR JSON blob
server.stop()
driver.quit()
file1 = open(file_path,'w')
json.dump(proxy.har,file1)
file1.close()
示例6: get_driver
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def get_driver(self, browser, start_beacon_url):
server = Server(BROWSERMOB_LOCATION)
server.start()
self.proxy = server.create_proxy()
driver = webdriver.Firefox(proxy=self.proxy.selenium_proxy())
self.proxy.new_har()
self.beacon_url = start_beacon_url
return driver
示例7: pytest_runtest_setup
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def pytest_runtest_setup(item):
logger = logging.getLogger(__name__)
if item.config.option.bmp_test_proxy and 'skip_browsermob_proxy' not in item.keywords:
if hasattr(item.session.config, 'browsermob_server'):
server = item.session.config.browsermob_server
else:
server = Server(item.config.option.bmp_path, {'port': int(item.config.option.bmp_port)})
item.config.browsermob_test_proxy = server.create_proxy()
logger.info('BrowserMob test proxy started (%s:%s)' % (item.config.option.bmp_host, item.config.browsermob_test_proxy.port))
configure_browsermob_proxy(item.config.browsermob_test_proxy, item.config)
#TODO make recording of har configurable
item.config.browsermob_test_proxy.new_har()
示例8: init_proxy_server
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def init_proxy_server(self, port=None):
kwargs = {}
if port is not None:
kwargs['port'] = port
if self.chained_proxy is not None:
if self.is_https:
kwargs['httpsProxy'] = self.chained_proxy
else:
kwargs['httpProxy'] = self.chained_proxy
if self.proxy_username is not None:
kwargs['proxyUsername'] = self.proxy_username
if self.proxy_password is not None:
kwargs['proxyPassword'] = self.proxy_password
server = Server('C://browsermob-proxy//bin//browsermob-proxy.bat', options={"port": self.server_port})
server.start()
proxy = server.create_proxy(params=kwargs)
return server, proxy
示例9: main
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def main():
init()
if len(sys.argv) >= 2:
start_url = sys.argv[1]
else:
print "You must specify page URL!"
sys.exit()
allowed_domain = urlparse(start_url).netloc
browsermobproxy_path = "/usr/local/opt/browsermobproxy/bin/browsermob-proxy"
options = {
'port': 9090,
}
server = Server(browsermobproxy_path,options)
server.start()
proxy = server.create_proxy()
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(start_url)
links = driver.find_elements_by_tag_name('a')
lenl = len(links)
for i in range(0,lenl):
if links[i].is_displayed():
url = links[i].get_attribute('href')
text = links[i].get_attribute('text')
if url.find(allowed_domain) != -1:
proxy.new_har('demo')
links[i].click()
print "%s Clicked on the link '%s' with HREF '%s'" % (Fore.BLUE+"*"+Fore.RESET,Style.BRIGHT+text+Style.RESET_ALL,Style.BRIGHT+url+Style.RESET_ALL)
show_status_codes(proxy.har,allowed_domain)
driver.back()
driver.refresh()
links = driver.find_elements_by_tag_name('a')
driver.quit()
server.stop()
示例10: fetch
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def fetch(url, config, output_directory, fetchEngine="browsermobproxy+selenium", browser="firefox"):
if fetchEngine in ("phantomjs", "ph"):
data = subprocess.check_output( config['fetchEngines']['phantomjs_command'].replace("$url", url), shell=True )
elif fetchEngine in ("browsermobproxy+selenium", "bs"):
from browsermobproxy import Server
from selenium import webdriver
server = Server(config['fetchEngines']['browsermobproxy_binary'])
server.start()
proxy = server.create_proxy()
if browser in ("firefox", "ff"):
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
else:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))
driver = webdriver.Chrome(chrome_options = chrome_options)
proxy.new_har(url, options={'captureHeaders': True})
driver.get(url)
data = json.dumps(proxy.har, ensure_ascii=False)
server.stop()
driver.quit()
else:
sys.exit("Unrecognized engine.")
if (data):
fileName = output_directory + "/" + url.replace("http://", "").replace("https://", "") + "_" + strftime("%Y-%m-%d_%H:%M:%S", gmtime()) + ".har"
f = open(fileName, "w")
f.write(data.encode("utf8"))
f.close()
return fileName
else:
return None
示例11: CreateHar
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
class CreateHar(object):
"""create HTTP archive file"""
def __init__(self, mob_path):
"""initial setup"""
self.browser_mob = mob_path
self.server = self.driver = self.proxy = None
@staticmethod
def __store_into_file(title, result):
"""store result"""
har_file = open(title + '.har', 'w')
har_file.write(str(result))
har_file.close()
def __start_server(self):
"""prepare and start server"""
self.server = Server(self.browser_mob)
self.server.start()
self.proxy = self.server.create_proxy()
def __start_driver(self):
"""prepare and start driver"""
profile = webdriver.FirefoxProfile()
profile.set_proxy(self.proxy.selenium_proxy())
self.driver = webdriver.Firefox(firefox_profile=profile)
def start_all(self):
"""start server and driver"""
self.__start_server()
self.__start_driver()
def create_har(self, title, url):
"""start request and parse response"""
self.proxy.new_har(title)
self.driver.get(url)
result = json.dumps(self.proxy.har, ensure_ascii=False)
self.__store_into_file(title, result)
def stop_all(self):
"""stop server and driver"""
self.server.stop()
self.driver.quit()
示例12: get_driver_and_proxy
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def get_driver_and_proxy():
global display
global driver
global proxy
if not driver:
if int(config.get('browsermob', {}).get('collect-har', 0)):
from browsermobproxy import Server
server = Server(config['browsermob']['path'])
server.start()
proxy = server.create_proxy()
if int(config.get('xconfig', {}).get('headless', 0)):
display = Display(visible=0, size=(800, 600))
display.start()
profile = webdriver.FirefoxProfile()
if proxy:
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
driver.implicitly_wait(60)
return driver, proxy
示例13: ad_driver
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
class ad_driver():
_driver = None
_server = None
_proxy = None
def __init__(self, path_to_batch, browser="chrome"):
""" start browsermob proxy """
self._server = Server(path_to_batch)
self._server.start()
self._proxy = self._server.create_proxy()
""" Init browser profile """
if browser is "chrome":
PROXY = "localhost:%s" % self._proxy.port # IP:PORT or HOST:PORT
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % PROXY)
self._driver = webdriver.Chrome(chrome_options=chrome_options)
elif browser is "ff":
profile = webdriver.FirefoxProfile()
driver = webdriver.Firefox(firefox_profile=profile, proxy=proxy)
else:
print "Please set 'browser' variable to any of the value \n 'chrome', 'ff' !"
self._driver.maximize_window()
self._driver.implicitly_wait(20)
def execute(self, test):
self._proxy.new_har(test["name"])
self._driver.get(_test_data_dir + os.sep + test['file'])
time.sleep(2)
callToTestMethod = getattr(test_steps, test["name"])
callToTestMethod(self._driver)
har = self._proxy.har
requests = har['log']['entries']
return requests
def quit(self):
self._server.stop()
self._driver.quit()
示例14: Proxy
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
class Proxy(object):
proxy = None
proxy_server = None
test_id = None
def __init__(self, test_id):
self.test_id = test_id
self.start_proxy()
def start_proxy(self):
self.proxy_server = Server(config.proxy_bin)
self.proxy_server.start()
self.proxy = self.proxy_server.create_proxy()
if config.blacklist:
self.set_blacklist(config.blacklist)
self.proxy.new_har(self.test_id)
logger.debug('Browsermob proxy started.')
return self
def stop_proxy(self):
filename = '{}.har'.format(self.test_id)
with open(filename, 'w') as harfile:
json.dump(self.proxy.har, harfile)
data = json.dumps(self.proxy.har, ensure_ascii=False)
self.proxy_server.stop()
self.proxy = None
self.proxy_server = None
logger.debug('Browsermob proxy stopped. HAR created: {}'
.format(filename))
def set_blacklist(self, domain_list):
for domain in domain_list:
self.proxy.blacklist("^https?://([a-z0-9-]+[.])*{}*.*"
.format(domain), 404)
logger.debug("Proxy blacklist set.")
def get_blacklist(self):
return requests.get('{}{}/blacklist'
.format(config.proxy_api, self.proxy.port))
示例15: _setup_proxy_server
# 需要导入模块: from browsermobproxy import Server [as 别名]
# 或者: from browsermobproxy.Server import create_proxy [as 别名]
def _setup_proxy_server(self, downstream_kbps=None, upstream_kbps=None,
latency=None):
server = Server(BROWSERMOB_PROXY_PATH)
server.start()
proxy = server.create_proxy()
# The proxy server is pretty sluggish, setting the limits might not
# achieve the desired behavior.
proxy_options = {}
if downstream_kbps:
proxy_options['downstream_kbps'] = downstream_kbps
if upstream_kbps:
proxy_options['upstream_kbps'] = upstream_kbps
if latency:
proxy_options['latency'] = latency
if len(proxy_options.items()) > 0:
proxy.limits(proxy_options)
return server, proxy