python请求可以具有每个会话的钩子还是全局钩子?

python请求可以具有每个会话的钩子还是全局钩子?,python,hook,python-requests,portforwarding,Python,Hook,Python Requests,Portforwarding,我有一个计划要用 我想使用请求联系那些被转发的服务器 我曾想过使用请求挂钩,但我不想在每个请求中都传递它们 我想要这样的全局/会话: import requests def forwarder_pre_request_hook(req, *args, **args): # do the trick I want, and replace the hostname and port of the url # I would still need to figure howto do

我有一个计划要用

我想使用请求联系那些被转发的服务器 我曾想过使用请求挂钩,但我不想在每个请求中都传递它们

我想要这样的全局/会话:

import requests

def forwarder_pre_request_hook(req, *args, **args):
   # do the trick I want, and replace the hostname and port of the url
   # I would still need to figure howto do it.
   return req

# globally 
requests.hooks = dict(pre_request=forwarder_pre_request_hook)

# per session
requests.Session(hooks=dict(pre_request=forwarder_pre_request_hook))

可能吗?

看起来我没有仔细阅读文档。似乎很有可能您需要更仔细地阅读: