本文整理汇总了Python中requests_toolbelt.utils.dump.dump_all方法的典型用法代码示例。如果您正苦于以下问题:Python dump.dump_all方法的具体用法?Python dump.dump_all怎么用?Python dump.dump_all使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests_toolbelt.utils.dump
的用法示例。
在下文中一共展示了dump.dump_all方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: request
# 需要导入模块: from requests_toolbelt.utils import dump [as 别名]
# 或者: from requests_toolbelt.utils.dump import dump_all [as 别名]
def request(self, method, url, *args, **kwargs):
response = None
use_cache = not self.no_cache and self._cache_responses
if use_cache:
logger.debug("getting cached response fsesor %s" %(url))
self.cursor.execute(
"SELECT response, last_seen "
"FROM response_cache "
"WHERE url = ?",
(url,)
)
try:
(pickled_response, last_seen) = self.cursor.fetchone()
td = datetime.now() - last_seen
if td.seconds >= self._cache_responses:
logger.debug("cache expired for %s" %(url))
else:
response = pickle.loads(pickled_response)
logger.debug("using cached response for %s" %(url))
except TypeError:
logger.debug("no cached response for %s" %(url))
# if not response:
# response = method(url, *args, **kwargs)
# logger.trace(dump.dump_all(response).decode("utf-8"))
if use_cache:
pickled_response = pickle.dumps(response)
sql="""INSERT OR REPLACE
INTO response_cache (url, response, last_seen)
VALUES (?, ?, ?)"""
self.cursor.execute(
sql,
(url, pickled_response, datetime.now())
)
self.conn.commit()
return response
示例2: debugRequest
# 需要导入模块: from requests_toolbelt.utils import dump [as 别名]
# 或者: from requests_toolbelt.utils.dump import dump_all [as 别名]
def debugRequest(req):
try:
print(dump.dump_all(req).decode('utf-8'))
except ValueError as e:
print("Debug Error: {}".format(getattr(e, 'message', e)))
# ------------------------------------------------------------------------------- #
# Unescape / decode html entities
# ------------------------------------------------------------------------------- #
示例3: dump_all
# 需要导入模块: from requests_toolbelt.utils import dump [as 别名]
# 或者: from requests_toolbelt.utils.dump import dump_all [as 别名]
def dump_all(response, request_prefix=b'< ', response_prefix=b'> '):
"""Dump all requests and responses including redirects.
This takes the response returned by requests and will dump all
request-response pairs in the redirect history in order followed by the
final request-response.
Example::
import requests
from requests_toolbelt.utils import dump
resp = requests.get('https://httpbin.org/redirect/5')
data = dump.dump_all(resp)
print(data.decode('utf-8'))
:param response:
The response to format
:type response: :class:`requests.Response`
:param request_prefix: (*optional*)
Bytes to prefix each line of the request data
:type request_prefix: :class:`bytes`
:param response_prefix: (*optional*)
Bytes to prefix each line of the response data
:type response_prefix: :class:`bytes`
:returns: Formatted bytes of request and response information.
:rtype: :class:`bytearray`
"""
data = bytearray()
history = list(response.history[:])
history.append(response)
for response in history:
dump_response(response, request_prefix, response_prefix, data)
return data
示例4: _dump_request
# 需要导入模块: from requests_toolbelt.utils import dump [as 别名]
# 或者: from requests_toolbelt.utils.dump import dump_all [as 别名]
def _dump_request(response):
data = dump.dump_all(response)
try:
logger.trace(data.decode())
except Exception:
data = str(data).replace('\\r\\n', '\r\n')
logger.trace(data)
示例5: log_request_response
# 需要导入模块: from requests_toolbelt.utils import dump [as 别名]
# 或者: from requests_toolbelt.utils.dump import dump_all [as 别名]
def log_request_response(resp, *args, **kwargs):
if resp.url.startswith(config.api_server):
return # k8s library already does its own dumping, we don't need to do it here
log = logging.getLogger(__name__)
data = dump_all(resp, "<<<", ">>>")
log.debug("Request/Response\n" + data)
示例6: _log_response
# 需要导入模块: from requests_toolbelt.utils import dump [as 别名]
# 或者: from requests_toolbelt.utils.dump import dump_all [as 别名]
def _log_response(self, response, message):
try:
msg = "[{}] {} | {} {}".format(response.request.method, message, response.status_code, response.reason)
if response.status_code == requests.codes.ok or response.status_code >= requests.codes.server_error:
self.log(msg)
else:
self.log("{} ({})".format(msg, response.json()["errors"]), level=logging.ERROR)
self._logger.debug("DUMP:\n{}".format(dump.dump_all(response).decode("utf-8")))
except (UnicodeDecodeError, JSONDecodeError) as e:
self._logger.debug("DUMP:\nImpossible to decode.")
示例7: debugRequest
# 需要导入模块: from requests_toolbelt.utils import dump [as 别名]
# 或者: from requests_toolbelt.utils.dump import dump_all [as 别名]
def debugRequest(req):
try:
print(dump.dump_all(req).decode('utf-8'))
except: # noqa
pass
##########################################################################################################################################################
示例8: dump_response
# 需要导入模块: from requests_toolbelt.utils import dump [as 别名]
# 或者: from requests_toolbelt.utils.dump import dump_all [as 别名]
def dump_response(self, response):
data = dump.dump_all(response)
return str(data.decode('utf-8'))