在 Python 中设置 getaddrinfo() 超时

发布于 2024-09-25 00:16:03 字数 381 浏览 1 评论 0原文

是否可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2024-10-02 00:16:03

我的理解是 getaddrinfo 是操作系统提供的库的包装:

在 unix 上:

int getaddrinfo(const char *nodename, const char *servname,
                const struct addrinfo *hints, struct addrinfo **res);

在 Windows 上:

int WSAAPI getaddrinfo(
  __in_opt  PCSTR pNodeName,
  __in_opt  PCSTR pServiceName,
  __in_opt  const ADDRINFOA *pHints,
  __out     PADDRINFOA *ppResult
);

它们都没有使用超时值,因此 Python 无法直接为您提供。
您必须使用线程进行编码或使用第三方库。

My understanding is that getaddrinfo is wrapper over OS provided library:

On unix:

int getaddrinfo(const char *nodename, const char *servname,
                const struct addrinfo *hints, struct addrinfo **res);

On Windows:

int WSAAPI getaddrinfo(
  __in_opt  PCSTR pNodeName,
  __in_opt  PCSTR pServiceName,
  __in_opt  const ADDRINFOA *pHints,
  __out     PADDRINFOA *ppResult
);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文