将超时传递给socket.getaddrinfo [英] Pass timeout to socket.getaddrinfo

查看:80
本文介绍了将超时传递给socket.getaddrinfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在用Python编写HTTP请求,而我的DNS服务器出现故障.

Let's say I'm writing HTTP requests in Python and my DNS server goes down.

如果我尝试:

import requests
requests.get('https://api.twilio.com', timeout=3)

并且DNS服务器已关闭,尽管指定了超时值,但这可能最多需要90秒.

and the DNS server is down, this can take upwards of 90 seconds, despite specifying a timeout value.

此外,阻塞调用是 socket.getaddrinfo ,看起来好像没有超时参数.

Furthermore the blocking call is socket.getaddrinfo, and it doesn't look like this takes a timeout parameter.

是否可以在DNS查找上设置超时?

Is there a way to set a timeout on the DNS lookup?

推荐答案

无法将超时传递给getaddrinfo系统调用,因为它不接受超时参数.您可以在线程中运行查找,如果过期则将其取消.

There is no way to pass a timeout to the getaddrinfo system call, because it does not accept a timeout argument. You can run the lookup in a thread, and then cancel it if it expires.

在Unix机器上,您可以将/etc/resolv.conf 修改为

On Unix machines, you can modify /etc/resolv.conf to set a timeout for addrinfo lookups.

这篇关于将超时传递给socket.getaddrinfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆