如何覆盖第三方库使用的“全局\u默认\u超时”?

2024-04-29 11:04:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用tavern测试python flask应用程序。偶尔,我会遇到http连接错误,如:

for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
    af, socktype, proto, canonname, sa = res
    sock = None
    try:
        sock = socket.socket(af, socktype, proto)

        # If provided, set socket level options before connecting.
        _set_socket_options(sock, socket_options)

        if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
            sock.settimeout(timeout)
        if source_address:
            sock.bind(source_address)
>       sock.connect(sa)
E       **ConnectionRefusedError: [Errno 111] Connection refused**

一个可能的原因是连接超时,所以我想更改tavern使用的http连接的超时。我无法从tavern中找到用于更改此值的配置,但根据上面的源代码,似乎默认超时是从socket._GLOBAL_DEFAULT_TIMEOUT读取的。如何从我的应用程序更改此值?我不直接调用这个由Tavern框架调用的连接方法。有没有办法从我的应用程序端更新它? 我正在使用pytest来运行测试,因此如果我可以在pytest上设置一些内容来覆盖这个值,这也很好


Tags: 应用程序httpifsatimeoutressocketglobal