本文整理汇总了Python中tornado.httpclient.HTTPResponse方法的典型用法代码示例。如果您正苦于以下问题:Python httpclient.HTTPResponse方法的具体用法?Python httpclient.HTTPResponse怎么用?Python httpclient.HTTPResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.httpclient
的用法示例。
在下文中一共展示了httpclient.HTTPResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _handle_exception
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPError(599, "Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
示例2: get_request
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def get_request(self, url, headers=None):
response = None
http_client = AsyncHTTPClient()
try:
# response = yield http_client.fetch(url, method='GET', headers=headers,request_timeout=5)
# POST
body = {'a': 4, 'b': 5}
response: HTTPResponse = yield http_client.fetch(url, method='POST', body=str(body), headers=headers, request_timeout=5)
except Exception as e:
print('get_request error:{0}'.format(e))
#请求
response.request
#请求头
response.headers
response_body = None
if response and response.code == 200:
# print('fetched {0}'.format(url))
response_body = response.body if isinstance(response.body, str) \
else response.body.decode()
return response_body
示例3: _on_request_token
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _on_request_token(
self,
authorize_url: str,
callback_uri: Optional[str],
response: httpclient.HTTPResponse,
) -> None:
handler = cast(RequestHandler, self)
request_token = _oauth_parse_response(response.body)
data = (
base64.b64encode(escape.utf8(request_token["key"]))
+ b"|"
+ base64.b64encode(escape.utf8(request_token["secret"]))
)
handler.set_cookie("_oauth_request_token", data)
args = dict(oauth_token=request_token["key"])
if callback_uri == "oob":
handler.finish(authorize_url + "?" + urllib.parse.urlencode(args))
return
elif callback_uri:
args["oauth_callback"] = urllib.parse.urljoin(
handler.request.full_url(), callback_uri
)
handler.redirect(authorize_url + "?" + urllib.parse.urlencode(args))
示例4: _handle_exception
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
))
if hasattr(self, "stream"):
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along.
return False
示例5: _on_timeout
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _on_timeout(self, key, info=None):
"""Timeout callback of request.
Construct a timeout HTTPResponse when a timeout occurs.
:arg object key: A simple object to mark the request.
:info string key: More detailed timeout information.
"""
request, callback, timeout_handle = self.waiting[key]
self.queue.remove((key, request, callback))
error_message = "Timeout {0}".format(info) if info else "Timeout"
timeout_response = HTTPResponse(
request, 599, error=HTTPTimeoutError(error_message),
request_time=self.io_loop.time() - request.start_time)
self.io_loop.add_callback(callback, timeout_response)
del self.waiting[key]
示例6: _handle_exception
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _handle_exception(self, typ, value, tb):
if self.final_callback:
self._remove_timeout()
if isinstance(value, StreamClosedError):
if value.real_error is None:
value = HTTPStreamClosedError("Stream closed")
else:
value = value.real_error
self._run_callback(HTTPResponse(self.request, 599, error=value,
request_time=self.io_loop.time() - self.start_time,
start_time=self.start_wall_time,
))
if hasattr(self, "stream"):
# TODO: this may cause a StreamClosedError to be raised
# by the connection's Future. Should we cancel the
# connection more gracefully?
self.stream.close()
return True
else:
# If our callback has already been called, we are probably
# catching an exception that is not caused by us but rather
# some child of our callback. Rather than drop it on the floor,
# pass it along, unless it's just the stream being closed.
return isinstance(value, StreamClosedError)
示例7: add_host
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def add_host(self, host, paths):
"""Add a host whose requests should be mocked.
Args:
host (str): the host to mock (e.g. 'api.github.com')
paths (list(str|regex, callable)): a list of paths (or regexps for paths)
and callables to be called for those paths.
The mock handlers will receive the request as their only argument.
Mock handlers can return:
- None
- int (empty response with this status code)
- str, bytes for raw response content (status=200)
- list, dict for JSON response (status=200)
- HTTPResponse (passed unmodified)
Example::
client.add_host('api.github.com', [
('/user': lambda request: {'login': 'name'})
])
"""
self.hosts[host] = paths
示例8: fetch_mock
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def fetch_mock(self, request):
"""Fetch a mocked request
Arguments:
request (HTTPRequest): the request to fetch
Returns:
HTTPResponse constructed from the info stored in the mocks
Raises:
HTTPError if the cached response has status >= 400
"""
mock_data = self.mocks[self.url_key(request.url)]
code = mock_data.get('code', 200)
headers = HTTPHeaders(mock_data.get('headers', {}))
response = HTTPResponse(request, code, headers=headers)
response.buffer = io.BytesIO(mock_data['body'].encode('utf8'))
if code >= 400:
raise HTTPError(mock_data['code'], response=response)
return response
示例9: protocol_switcher
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def protocol_switcher(request):
try:
host = request.headers['Host']
except KeyError:
# We don't have FQDN. Fallback to socket address. This breaks
# name-based virtualhost.
host = '%(address)s:%(port)s' % dict(
request.config.temboard, address=request.host)
new_url = 'https://%s%s' % (host, request.uri)
headers = HTTPHeaders({
'Content-Length': '0',
'Location': new_url,
})
logger.debug("Redirecting client to %s.", new_url)
return HTTPResponse(
request=request, code=301, headers=headers,
# If effective_url is not set, HTTPResponse falls back to request.url,
# which does not exists... See tornado.httpclient.HTTPResponse.__init__
# and tornado.httpserver.HTTPRequest.
effective_url=request.full_url(),
)
示例10: _on_timeout
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _on_timeout(self, key):
request, callback, timeout_handle = self.waiting[key]
self.queue.remove((key, request, callback))
timeout_response = HTTPResponse(
request, 599, error=HTTPError(599, "Timeout"),
request_time=self.io_loop.time() - request.start_time)
self.io_loop.add_callback(callback, timeout_response)
del self.waiting[key]
示例11: _process_queue
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _process_queue(self):
with stack_context.NullContext():
while True:
started = 0
while self._free_list and self._requests:
started += 1
curl = self._free_list.pop()
(request, callback) = self._requests.popleft()
curl.info = {
"headers": httputil.HTTPHeaders(),
"buffer": BytesIO(),
"request": request,
"callback": callback,
"curl_start_time": time.time(),
}
try:
self._curl_setup_request(
curl, request, curl.info["buffer"],
curl.info["headers"])
except Exception as e:
# If there was an error in setup, pass it on
# to the callback. Note that allowing the
# error to escape here will appear to work
# most of the time since we are still in the
# caller's original stack frame, but when
# _process_queue() is called from
# _finish_pending_requests the exceptions have
# nowhere to go.
callback(HTTPResponse(
request=request,
code=599,
error=e))
else:
self._multi.add_handle(curl)
if not started:
break
示例12: _finish
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _finish(self, curl, curl_error=None, curl_message=None):
info = curl.info
curl.info = None
self._multi.remove_handle(curl)
self._free_list.append(curl)
buffer = info["buffer"]
if curl_error:
error = CurlError(curl_error, curl_message)
code = error.code
effective_url = None
buffer.close()
buffer = None
else:
error = None
code = curl.getinfo(pycurl.HTTP_CODE)
effective_url = curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
# the various curl timings are documented at
# http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
time_info = dict(
queue=info["curl_start_time"] - info["request"].start_time,
namelookup=curl.getinfo(pycurl.NAMELOOKUP_TIME),
connect=curl.getinfo(pycurl.CONNECT_TIME),
pretransfer=curl.getinfo(pycurl.PRETRANSFER_TIME),
starttransfer=curl.getinfo(pycurl.STARTTRANSFER_TIME),
total=curl.getinfo(pycurl.TOTAL_TIME),
redirect=curl.getinfo(pycurl.REDIRECT_TIME),
)
try:
info["callback"](HTTPResponse(
request=info["request"], code=code, headers=info["headers"],
buffer=buffer, effective_url=effective_url, error=error,
reason=info['headers'].get("X-Http-Reason", None),
request_time=time.time() - info["curl_start_time"],
time_info=time_info))
except Exception:
self.handle_callback_exception(info["callback"])
示例13: _on_http_response
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _on_http_response(self, response: httpclient.HTTPResponse) -> None:
if not self.connect_future.done():
if response.error:
self.connect_future.set_exception(response.error)
else:
self.connect_future.set_exception(
WebSocketError("Non-websocket response")
)
示例14: _finish
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def _finish(self, curl, curl_error=None, curl_message=None):
info = curl.info
curl.info = None
self._multi.remove_handle(curl)
self._free_list.append(curl)
buffer = info["buffer"]
if curl_error:
error = CurlError(curl_error, curl_message)
code = error.code
effective_url = None
buffer.close()
buffer = None
else:
error = None
code = curl.getinfo(pycurl.HTTP_CODE)
effective_url = curl.getinfo(pycurl.EFFECTIVE_URL)
buffer.seek(0)
# the various curl timings are documented at
# http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
time_info = dict(
queue=info["curl_start_time"] - info["request"].start_time,
namelookup=curl.getinfo(pycurl.NAMELOOKUP_TIME),
connect=curl.getinfo(pycurl.CONNECT_TIME),
pretransfer=curl.getinfo(pycurl.PRETRANSFER_TIME),
starttransfer=curl.getinfo(pycurl.STARTTRANSFER_TIME),
total=curl.getinfo(pycurl.TOTAL_TIME),
redirect=curl.getinfo(pycurl.REDIRECT_TIME),
)
try:
info["callback"](HTTPResponse(
request=info["request"], code=code, headers=info["headers"],
buffer=buffer, effective_url=effective_url, error=error,
request_time=time.time() - info["curl_start_time"],
time_info=time_info))
except Exception:
self.handle_callback_exception(info["callback"])
示例15: test_str
# 需要导入模块: from tornado import httpclient [as 别名]
# 或者: from tornado.httpclient import HTTPResponse [as 别名]
def test_str(self):
response = HTTPResponse(HTTPRequest('http://example.com'),
200, headers={}, buffer=BytesIO())
s = str(response)
self.assertTrue(s.startswith('HTTPResponse('))
self.assertIn('code=200', s)