當前位置: 首頁>>代碼示例>>Python>>正文


Python Server.create_proxy方法代碼示例

本文整理匯總了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()
開發者ID:alder,項目名稱:woodpycker,代碼行數:62,代碼來源:woodpycker.py

示例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()
開發者ID:je-nunez,項目名稱:create_and_analyze_HAR_HTML_archive,代碼行數:32,代碼來源:generate_har_file.py

示例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)
開發者ID:michaelliu2,項目名稱:webscrape_script,代碼行數:30,代碼來源:scrape___data.py

示例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)
開發者ID:rishimaths,項目名稱:bok-choy,代碼行數:31,代碼來源:performance.py

示例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()
開發者ID:sachin6757,項目名稱:CaptureNetworkTraffic,代碼行數:30,代碼來源:CaptureNetworkTraffic.py

示例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
開發者ID:etitolo,項目名稱:WSI,代碼行數:10,代碼來源:pmct.py

示例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()
開發者ID:Ratler,項目名稱:pytest-browsermob-proxy,代碼行數:17,代碼來源:pytest_browsermob_proxy.py

示例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
開發者ID:wangwei0807,項目名稱:cv_crawler,代碼行數:19,代碼來源:seleniumLogin.py

示例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()
開發者ID:eremv,項目名稱:woodpycker,代碼行數:46,代碼來源:woodpycker.py

示例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
開發者ID:renekliment,項目名稱:har-tree-viewer,代碼行數:45,代碼來源:fetcher.py

示例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()
開發者ID:ysya,項目名稱:Domain-Parking-Sensors-Auto-Script,代碼行數:45,代碼來源:getHar.py

示例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
開發者ID:beckastar,項目名稱:cleaner_markov,代碼行數:22,代碼來源:__init__.py

示例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()
開發者ID:pgurumukhi,項目名稱:ConverseAnalytics,代碼行數:42,代碼來源:ad_driver.py

示例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))
開發者ID:DramaFever,項目名稱:sst,代碼行數:42,代碼來源:proxy.py

示例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
開發者ID:MinhHuong,項目名稱:oppia,代碼行數:25,代碼來源:perf_services.py


注:本文中的browsermobproxy.Server.create_proxy方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。