def do_request(self, agent, method, addr, body, handler, token=None): if token: headers = Headers({"Authorization": ["Bearer " + self.jwt_token]}) else: headers = None response = yield agent.request(method, addr, headers, bodyProducer=body) yield self.response_handler(response, handler)
def perform_request(attempts): uri = urljoin(self.base_address, urn) d = agent.request(method, uri, bodyProducer=bodyProducer, headers=Headers(self.base_headers)) @d.addCallback def callback(response): try: content_length = int( response.headers.getRawHeaders('content-length')[0]) except: content_length = None response.deliverBody(genReceiver(finished, content_length)) def errback(err, attempts): # We we will recursively keep trying to perform a request until # we have reached the retry count. if attempts < retries: log.err("Lookup failed. Retrying.") attempts += 1 perform_request(attempts) else: log.err("Failed. Giving up.") finished.errback(err) d.addErrback(errback, attempts)
def request(self, uri, callback, options={}): self._agent.request( 'POST', uri.encode("utf-8"), Headers({'Content-Type': ['application/json']}), StringBodyProducer(json.dumps(options)), ).addCallbacks(self._onResponse, errback=self._onReloadError, callbackArgs=(callback, ))
def requestAvatarId(self, credentials): # 'credentials' is a single string, the browserid assertion coming # from the web client. We send it to the online verifier service for # checking. In the long run (i.e. after the protocol has settled # down), it is better (i.e. removes browserid.org from the reliance # set) to perform the cryptographic verification locally. This will # require fetching a pubkey from the IdP's /.well-known/ directory # and performing signature checking with the result. body = json.dumps({"assertion": credentials.assertion, "audience": self.audience}) headers = Headers() headers.addRawHeader("Content-Type", "application/json") d = self.agent.request("POST", self.verifier_url, headers=headers, bodyProducer=MemoryBodyProducer(body)) d.addCallback(getBody) d.addCallback(json.loads) d.addCallback(self._parse_response) return d
def get_body_from_request(path, data): from twisted.internet import reactor jsondata = FileBodyProducer(StringIO(json.dumps(data))) agent = Agent(reactor) d = agent.request('POST', POINTTRADER_SERVER + path, Headers({'Content-Type': ['application/json']}), jsondata) d.addCallback(get_body) return d
def get_body_from_request(path, data): from twisted.internet import reactor jsondata = FileBodyProducer(StringIO(json.dumps(data))) agent = Agent(reactor) d = agent.request('POST', conf.settings['pointtrader_server'] + path, Headers({'Content-Type': ['application/json']}), jsondata) d.addCallback(get_body) return d
def test_header_isolation(self): agent = CassetteAgent(self.agent, '') finished = agent.request('GET', 'http://foo.test/') request, result = self.protocol.requests.pop() headers = Headers() headers.addRawHeader('Content-Encoding', 'gzip') response = Response._construct(('HTTP', 1, 1), 200, 'OK', headers, AbortableStringTransport(), request) response._bodyDataFinished() result.callback(response) @inlineCallbacks def assert_intact_headers(agent_response): yield readBody(agent_response) agent_response.headers.removeHeader('Content-Encoding') interaction = agent.cassette.as_dict()['http_interactions'][0] self.assertEqual( ['gzip'], interaction['response']['headers']['Content-Encoding']) returnValue(agent_response) finished.addCallback(assert_intact_headers) return finished
def asyncSend(self, data): """ Asynchronously send some data to the endpoint URL as an HTTP PUT. """ response = yield self.agent.request( "PUT", self.baseURL, Headers({"Content-Type": [self.encoder.contentType()]}), FileBodyProducer(StringIO(data))) out = StringIO() done = Deferred() class Catcher(Protocol): dataReceived = out.write def connectionLost(self, reason): if reason.check(ResponseDone): done.callback(out.getvalue()) else: done.errback(reason) response.deliverBody(Catcher()) returnValue((yield done))
def __init__(self, perfmon, server_name, server_address, server_port, username, password, verify, included_objects): self.perfmon = perfmon self.server_name = server_name self.server_address = server_address self.server_port = server_port self.username = username self.password = password self.verify = verify self.included_objects = included_objects self.authorization = base64.b64encode('{}:{}'.format(self.username, self.password).encode('utf-8')) self.headers = Headers({b'Content-Type': [b'text/xml; charset=utf-8'], b'Authorization': [b'Basic ' + self.authorization]}) self.agent = RateLimitAgent() if not self.verify: self.agent.contextFactory.good_domains.append(self.server_address.encode('utf-8')) self.url = URL.fromText('https://{server_address:}:{server_port:}/perfmonservice2/services/PerfmonService?wsdl'.format(server_address=server_address, server_port=server_port)) self.openSession()
b'Accept-Language': [b'zh-CN,zh;q=0.9'], b'Host': [b'www.baidu.com'], } headers1 = InsensitiveDict() headers1[ b'User-Agent'] = b'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36' headers1[b'Upgrade-Insecure-Requests'] = b'1' headers1[ b'Accept'] = b'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' headers1[b'Accept-Encoding'] = b'gzip, deflate, br' headers1[b'Accept-Language'] = b'zh-CN,zh;q=0.9' headers1[b'Host'] = b'www.baidu.com' headers2 = Headers(rawHeaders=headers) resposne1 = requests.get(url=url1) print(resposne1.text) d1 = getPage(bytes(url1, encoding='utf-8')) def showresult(data): print(data) reactor.stop() d1.addCallback(showresult) reactor.run()
def defaultHeaders(): headers = Headers() headers.addRawHeader('User-Agent', 'IPtvDream-%s/%s' % (NAME, VERSION)) return headers