本文整理汇总了Python中tornado.httpclient.HTTPRequest.headers方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPRequest.headers方法的具体用法?Python HTTPRequest.headers怎么用?Python HTTPRequest.headers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.httpclient.HTTPRequest
的用法示例。
在下文中一共展示了HTTPRequest.headers方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, request, callback, **kwargs):
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
# We're going to modify this (to add Host, Accept-Encoding, etc),
# so make sure we don't modify the caller's object. This is also
# where normal dicts get converted to HTTPHeaders objects.
request.headers = httputil.HTTPHeaders(request.headers)
callback = stack_context.wrap(callback)
key = object()
self.queue.append((key, request, callback))
if not len(self.active) < self.max_clients:
timeout_handle = self.io_loop.add_timeout(
time.time() + min(request.connect_timeout,
request.request_timeout),
functools.partial(self._on_timeout, key))
else:
timeout_handle = None
self.waiting[key] = (request, callback, timeout_handle)
self._process_queue()
if self.queue:
logging.debug(
'max_clients limit reached, request queued. '
'%d active, %d queued requests.' % (
len(self.active), len(self.queue))
)
示例2: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, request, callback, **kwargs):
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
if not isinstance(request.headers, HTTPHeaders):
request.headers = HTTPHeaders(request.headers)
callback = stack_context.wrap(callback)
_HTTPConnection(self.io_loop, request, callback)
示例3: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, request, callback, **kwargs):
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
if not isinstance(request.headers, HTTPHeaders):
request.headers = HTTPHeaders(request.headers)
callback = stack_context.wrap(callback)
self.queue.append((request, callback))
self._process_queue()
示例4: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, request, **kwargs):
headers = kwargs['headers'] if 'headers' in kwargs else default_headers
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, headers = headers, **kwargs)
request.headers = HTTPHeaders(request.headers if request.headers else headers)
try:
return self.http.fetch(request)
except (HTTPError, SyntaxError), e:
#todo log
print "[ERROR]", e
示例5: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, url, headers=None, body=None, method="GET", callback=None, raise_error=True, cache=None, **kwargs):
headers = headers or {}
body = body or "{}"
"""very simlar with AsyncHTTPClient.fetch
"""
if self._closed:
raise RuntimeError("fetch() called on closed AsyncHTTPClient")
future = TracebackFuture()
if isinstance(body, dict):
for k,v in body.items():
if v is None:
del body[k]
body = urllib.urlencode(body)
for k,v in headers.items(): #headers 只能接收str
if v:
headers[k] = str(headers[k])
else:
del headers[k]
request = HTTPRequest(url=url,method=method,headers=headers,body=body, allow_nonstandard_methods=True, request_timeout=600 ,**kwargs)
# We may modify this (to add Host, Accept-Encoding, etc),
# so make sure we don't modify the caller's object. This is also
# where normal dicts get converted to HTTPHeaders objects.
request.headers = httputil.HTTPHeaders(request.headers)
request = _RequestProxy(request, self.defaults)
if callback is not None:
callback = stack_context.wrap(callback)
def handle_future(future):
exc = future.exception()
if isinstance(exc, HTTPError) and exc.response is not None:
response = exc.response
elif exc is not None:
response = HTTPResponse(
request, 599, error=exc,
request_time=time.time() - request.start_time)
else:
response = future.result()
self.io_loop.add_callback(callback, response)
future.add_done_callback(handle_future)
def handle_response(response):
if raise_error and response.error:
future.set_exception(response.error)
else:
try:
resp = json.loads(str(response.body))
if resp.get("statusCode") and resp.get("statusCode")==800:
future.set_result(resp)
log.info(json.dumps({"response":resp,"body":body,"headers":headers,"url":url}))
else:
future.set_result({"error_type":"statusCode is not 800", "response":resp,"body":body,"headers":headers,"url":url})
log.error(json.dumps({"error_type":"statusCode is not 800", "response":resp,"body":body,"headers":headers,"url":url}))
except Exception,e:
future.set_result({"error_type":"json.loads failed!","error":str(e),"response.body":response.body,"body":body,"headers":headers,"url":url})
log.error(json.dumps({"error_type":"json.loads failed!","error":str(e),"response.body":response.body,"body":body,"headers":headers,"url":url}))
示例6: _request
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def _request(self, extension, body, callback):
"""Wraps tornado syntax for sending an HTTP request."""
request = HTTPRequest('http://{}/{}'.format(self.ip, extension),
connect_timeout=self.timeout,
request_timeout=self.timeout)
if body:
request.body = body
request.method = 'POST'
if extension == 'ToolWeb/Cmd':
request.headers = {'Content-Type': 'text/xml'}
self.client.fetch(request, callback)
示例7: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, request, callback, **kwargs):
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
if not isinstance(request.headers, HTTPHeaders):
request.headers = HTTPHeaders(request.headers)
callback = stack_context.wrap(callback)
self.queue.append((request, callback))
self._process_queue()
if self.queue:
logging.debug("max_clients limit reached, request queued. "
"%d active, %d queued requests." % (
len(self.active), len(self.queue)))
示例8: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, request, callback=None, raise_error=True, **kwargs):
"""Executes a request, asynchronously returning an `HTTPResponse`.
The request may be either a string URL or an `HTTPRequest` object.
If it is a string, we construct an `HTTPRequest` using any additional
kwargs: ``HTTPRequest(request, **kwargs)``
This method returns a `.Future` whose result is an
`HTTPResponse`. By default, the ``Future`` will raise an `HTTPError`
if the request returned a non-200 response code. Instead, if
``raise_error`` is set to False, the response will always be
returned regardless of the response code.
If a ``callback`` is given, it will be invoked with the `HTTPResponse`.
In the callback interface, `HTTPError` is not automatically raised.
Instead, you must check the response's ``error`` attribute or
call its `~HTTPResponse.rethrow` method.
"""
if self._closed:
raise RuntimeError("fetch() called on closed AsyncHTTPClient")
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
# We may modify this (to add Host, Accept-Encoding, etc),
# so make sure we don't modify the caller's object. This is also
# where normal dicts get converted to HTTPHeaders objects.
request.headers = httputil.HTTPHeaders(request.headers)
request = _RequestProxy(request, self.defaults)
future = TracebackFuture()
if callback is not None:
callback = stack_context.wrap(callback)
def handle_future(future):
exc = future.exception()
if isinstance(exc, HTTPError) and exc.response is not None:
response = exc.response
elif exc is not None:
response = HTTPResponse(
request, 599, error=exc,
request_time=time.time() - request.start_time)
else:
response = future.result()
self.io_loop.add_callback(callback, response)
future.add_done_callback(handle_future)
def handle_response(response):
if raise_error and response.error:
future.set_exception(response.error)
else:
future.set_result(response)
self.fetch_impl(request, handle_response)
return future
示例9: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, request, callback, **kwargs):
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
# We're going to modify this (to add Host, Accept-Encoding, etc),
# so make sure we don't modify the caller's object. This is also
# where normal dicts get converted to HTTPHeaders objects.
request.headers = HTTPHeaders(request.headers)
callback = stack_context.wrap(callback)
self.queue.append((request, callback))
self._process_queue()
if self.queue:
logging.debug("max_clients limit reached, request queued. "
"%d active, %d queued requests." % (
len(self.active), len(self.queue)))
示例10: send
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def send(self, message):
"""
Send a message to API URL
"""
send_url = '{}/{}'.format(self.url, self.get_api_path_equivalent(message=message))
logger.debug("EventReporter API URL = {}".format(send_url))
request = HTTPRequest(url=send_url)
request.method = 'POST'
request.headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
request.body = message.to_json()
response = None
try:
#noinspection PyTypeChecker
response = self.http_client.fetch(request=request)
logger.debug("Response from Event Reporter: {}".format(response.body))
except httpclient.HTTPError, e:
logger.error("Error when trying to contact Event Reporter! Error: {}".format(e))
示例11: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, request, callback, **kwargs):
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
if not isinstance(request.headers, HTTPHeaders):
request.headers = HTTPHeaders(request.headers)
callback = stack_context.wrap(callback)
self.queue.append((request, callback))
self._process_queue()
data = dict(request=request, callback=callback)
if self.queue:
logging.info("%smax_clients limit reached - request queued. "
"%d active, %d queued requests." % ( (self.log_name + ' ') if self.log_name else '',
len(self.active), len(self.queue)))
data['queued'] = True
else:
if options.verbose > 0:
logging.info('%s(%s-%s/%s) fetching %s' % ( (self.log_name + ' ') if self.log_name else '',
len(self.active), len(self.queue), self.max_clients,
request.url))
return data
示例12: fetch
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch(self, url, headers=None, body=None, method="GET", callback=None, raise_error=True, **kwargs):
"""very simlar with AsyncHTTPClient.fetch
"""
if self._closed:
raise RuntimeError("fetch() called on closed AsyncHTTPClient")
if isinstance(body, dict):
body = self.dict2form(body)
print (body)
request = HTTPRequest(url=url,method=method,headers=headers,body=body, allow_nonstandard_methods=True, **kwargs)
# We may modify this (to add Host, Accept-Encoding, etc),
# so make sure we don't modify the caller's object. This is also
# where normal dicts get converted to HTTPHeaders objects.
request.headers = httputil.HTTPHeaders(request.headers)
request = _RequestProxy(request, self.defaults)
future = TracebackFuture()
if callback is not None:
callback = stack_context.wrap(callback)
def handle_future(future):
exc = future.exception()
if isinstance(exc, HTTPError) and exc.response is not None:
response = exc.response
elif exc is not None:
response = HTTPResponse(
request, 599, error=exc,
request_time=time.time() - request.start_time)
else:
response = future.result()
self.io_loop.add_callback(callback, response)
future.add_done_callback(handle_future)
def handle_response(response):
if raise_error and response.error:
future.set_exception(response.error)
else:
try:
resp = json.loads(str(response.body))
future.set_result(resp)
except Exception,e:
print (e)
future.set_result({})
示例13: fetch_mock
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def fetch_mock(request, callback, **kwargs):
if not isinstance(request, HTTPRequest):
request = HTTPRequest(url=request, **kwargs)
request.headers = HTTPHeaders(request.headers)
output = cStringIO.StringIO()
if _OAUTH_REQUEST_TOKEN_URL in request.url:
fetch_mock_called[_OAUTH_REQUEST_TOKEN_URL] = True
output.write(
"oauth_token_secret={0}&oauth_token={1}".format(
oauth_request_token_secret, oauth_request_token))
resp = HTTPResponse(request=request, code=200, buffer=output)
elif _OAUTH_ACCESS_TOKEN_URL in request.url:
fetch_mock_called[_OAUTH_ACCESS_TOKEN_URL] = True
output.write(
"oauth_token_secret={0}&oauth_token={1}&uid={2}".format(
oauth_access_token_secret, oauth_access_token, uid))
resp = HTTPResponse(request=request, code=200, buffer=output)
elif _DROPBOX_ACCOUNT_INFO_URL in request.url:
fetch_mock_called[_DROPBOX_ACCOUNT_INFO_URL] = True
ret_body = json.dumps({
"referral_link": "https://www.dropbox.com/referrals/NTExMDMzMjU4OQ",
"display_name": first_name + ' ' + last_name,
"uid": uid,
"country": "RU",
"quota_info": {
"shared": 275506,
"quota": 6174015488,
"normal": 702636082},
"email": email,
})
output.write(ret_body)
resp = HTTPResponse(request=request, code=200, buffer=output)
else: # Unexpected url
resp = None
self.assertEqual(True, False)
callback(resp)
示例14: test_headers_setter
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def test_headers_setter(self):
request = HTTPRequest("http://example.com")
request.headers = {"bar": "baz"} # type: ignore
self.assertEqual(request.headers, {"bar": "baz"})
示例15: __build_auth_user_request
# 需要导入模块: from tornado.httpclient import HTTPRequest [as 别名]
# 或者: from tornado.httpclient.HTTPRequest import headers [as 别名]
def __build_auth_user_request(access_token):
httprequest = HTTPRequest(options.douban_auth_user)
httprequest.headers = {
"Authorization": 'Bearer ' + access_token
}
return httprequest