def retry(self, retry_count): """ Retries. No idea what its used for, but its in the API... :param retry_count: the number of retries """ r = request_1.put("%s/proxy/%s/retry" % (self.host, self.port), {"retrycount": retry_count}) return r.status_code
def whitelist(self, regexp, status_code): """ Sets a list of URL patterns to whitelist :param regex: a comma separated list of regular expressions :param status_code: the HTTP status code to return for URLs that do not \ match the whitelist """ r = request_1.put("%s/proxy/%s/whitelist" % (self.host, self.port), {"regex": regexp, "status": status_code}) return r.status_code
def rewrite_url(self, match, replace): """ Rewrites the requested url. :param match: a regex to match requests with :param replace: unicode \ a string to replace the matches with """ params = {"matchRegex": match, "replace": replace} r = request_1.put("%s/proxy/%s/rewrite" % (self.host, self.port), params) return r.status_code
def wait_for_traffic_to_stop(self, quiet_period, timeout): """ Waits for the network to be quiet :param quiet_period: number of miliseconds the network needs to be quiet for :param timeout: max number of miliseconds to wait """ r = request_1.put( "%s/proxy/%s/wait" % (self.host, self.port), {"quietPeriodInMs": quiet_period, "timeoutInMs": timeout} ) return r.status_code
def new_page(self, ref=None): """ This sets a new page to be recorded :param ref: A reference for the new page. Defaults to None """ if ref: payload = {"pageRef": ref} else: payload = {} r = request_1.put("%s/proxy/%s/har/pageRef" % (self.host, self.port), payload) return r.status_code
def limits(self, options): """ Limit the bandwidth through the proxy. :param options: A dictionary with all the details you want to set. \ downstreamKbps - Sets the downstream kbps \ upstreamKbps - Sets the upstream kbps \ latency - Add the given latency to each HTTP request """ params = {} for (k, v) in options.items(): if k not in self.LIMITS: raise KeyError("invalid key: %s" % k) params[self.LIMITS[k]] = int(v) if len(params.items()) == 0: raise KeyError("You need to specify one of the valid Keys") r = request_1.put("%s/proxy/%s/limit" % (self.host, self.port), params) return r.status_code
def new_har(self, ref=None, options={}): """ This sets a new HAR to be recorded :param ref: A reference for the HAR. Defaults to None :param options: A dictionary that will be passed to BrowserMob Proxy \ with specific keywords. Keywords are: \ captureHeaders - Boolean, capture headers \ captureContent - Boolean, capture content bodies \ captureBinaryContent - Boolean, capture binary content """ if ref: payload = {"initialPageRef": ref} else: payload = {} if options: payload.update(options) r = request_1.put("%s/proxy/%s/har" % (self.host, self.port), payload) if r.status_code == 200: return (r.status_code, r.json()) else: return (r.status_code, None)
def timeouts(self, options): """ Configure various timeouts in the proxy :param options: A dictionary with all the details you want to set. \ request - request timeout (in seconds) \ read - read timeout (in seconds) \ connection - connection timeout (in seconds) \ dns - dns lookup timeout (in seconds) """ params = {} for (k, v) in options.items(): if k not in self.TIMEOUTS: raise KeyError("invalid key: %s" % k) params[self.TIMEOUTS[k]] = int(v) if len(params.items()) == 0: raise KeyError("You need to specify one of the valid Keys") r = request_1.put("%s/proxy/%s/timeout" % (self.host, self.port), params) return r.status_code