![](/img/trans.png)
[英]How to requests.Session().get if website does not keep me logged in?
[英]Does requests.session expires if remained idle?
我在重试请求函数中使用 request.session() 。 我想知道会话持续多长时间。 如果session
对象保持空闲一段时间,它会在一段时间后过期吗?
def create_requests_retry_session(
retries=app.config["MAX_RETRY_FOR_SESSION"],
backoff_factor=app.config["BACKOFF_FACTOR"]):
"""
implement failure mechanism while calling microservices to avoid system alert
using parameters like a time limit to get a response. This function will attempt 3 times
per session
retries: Defaults to MAX_RETRY_FOR_SESSION
backoff_factor: how long the processes will sleep between failed requests.
Defaults to BACKOFF_FACTOR {backoff factor} * (2 ** ({number of total
retries} - 1)). If the backoff_factor is 0.1, then sleep() will sleep for [0.0s, 0.2s, 0.4s, …] between
retries. 1 second the successive sleeps will be 0.5, 1, 2, 4, 8, 16, 32, 64, 128, 256. 2 seconds - 1, 2, 4,
8, 16, 32, 64, 128, 256, 512 10 seconds - 5, 10, 20, 40, 80, 160, 320, 640, 1280, 2560
:return: Session object
"""
session = requests.Session()
retry = Retry(total=retries, backoff_factor=backoff_factor, method_whitelist=frozenset(['POST']))
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
return session
我使用上述函数中的会话对象作为 session.post()
会话将自动尝试尽可能长时间保持活动状态(除非明确禁用),直到它关闭。 任何超时都将是服务器强加的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.