Pass timeout to socket.getaddrinfo

与世无争的帅哥 提交于 2019-12-10 15:53:22

问题


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

If I try:

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

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

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

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


回答1:


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.

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



来源:https://stackoverflow.com/questions/26857922/pass-timeout-to-socket-getaddrinfo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!