在 Python 中设置 getaddrinfo() 超时
是否可以在 CPython 2.7 中的 getaddrinfo() 调用上设置超时?
socket.setdefaulttimeout() 确实不起作用。我真的不想要一个使用线程或信号包装函数的解决方案。仅使用标准库的解决方案是最好的,但使用第三方包也是可以接受的。
例如,我想这样做:
socket.getaddrinfo("""!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_""", None)
并让它在 1 秒内引发 socket.error。 (请注意,当我在 OS X 上运行它时,它很快就会超时,但在 Debian 上运行时,大约需要 60 秒才会失败)。
Is it possible to set a timeout on a getaddrinfo() call in CPython 2.7?
socket.setdefaulttimeout() does not work. I don't really want a solution that wraps a function using threads or signals. A solution that only uses the standard library is best, but using a third-party package would be acceptable.
For example, I want to do this:
socket.getaddrinfo("""!@#$%^&*()+=-[]\\\';,./{}|\":<>?~_""", None)
And have it raise a socket.error in 1 second. (Note that when I run this on OS X it times out rapidly anyway, but running on Debian it takes about 60 seconds to fail).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的理解是 getaddrinfo 是操作系统提供的库的包装:
在 unix 上:
在 Windows 上:
它们都没有使用超时值,因此 Python 无法直接为您提供。
您必须使用线程进行编码或使用第三方库。
My understanding is that getaddrinfo is wrapper over OS provided library:
On unix:
On Windows:
None of them uses has a timeout value and hence Python can not provide you directly.
You have to either code that using threads or use a third party library.