python - 套接字 python : recvfrom

标签 python sockets recvfrom

我想知道 python 中的 socket.recvfrom 是否是一个阻塞函数?我在 documentation 中找不到我的答案 如果不是,什么都没有收到会返回什么?一个空字符串 '' ? 在另一种情况下,如果实际上它是阻塞的,我该怎么做才能将其作为解锁功能呢?我听说了 settimeout但我不知道这是否真的是正确的解决方案。

最佳答案

默认情况下它是阻塞的。它可以通过 socket.setblocking(0) 或(等效地)socket.settimeout(0) 变成非阻塞的。在那种情况下,如果没有任何东西可以接收,它将抛出 socket.error 异常。请参阅文档:https://docs.python.org/2/library/socket.html#socket.socket.setblocking

关于python - 套接字 python : recvfrom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31166594/

相关文章:

Python 连接拒绝错误 : [Errno 61] Connection refused

c++ - 使用 MSG_DONTWAIT 的 C UDP 接收套接字总是失败

c - 使用 C 和 UDP 实现停止并等待,什么导致 'resource temporarily unavailable'?

python - 如何使用python复制文件以及目录结构/路径?

python - 对于从文件对话框加载图像有什么建议吗?

python - Python 中的正则表达式 : splitting on whitespace character in between two words that start with a capital letter

java - 套接字:BufferedReader readLine() block

c - 检测客户端 ctrl-c 套接字

c - UDP recvfrom查询

python - 错误 : Failed building wheel for pycryptodome