This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: socket.socket.setblocking does not raise exception if no data available
Type: Stage:
Components: Documentation Versions: Python 2.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Florian.Ludwig, docs@python, georg.brandl
Priority: normal Keywords:

Created on 2011-09-14 13:41 by Florian.Ludwig, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg144024 - (view) Author: Florian Ludwig (Florian.Ludwig) Date: 2011-09-14 13:41
The documentation states:

> In non-blocking mode, if a recv() call doesn’t find any data, [...], a error exception is raised; [0]

Which is wrong. If no data is available recv() does not raise an exception but returns an empty string.

[0] http://docs.python.org/library/socket.html#socket.socket.setblocking
msg144216 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-09-17 18:27
I think you're mistaking a closed connection with "no data available".

Small demo that this works as intended:

>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect(('localhost', 1234))
>>> s.recv(10)
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> s.setblocking(False)
>>> s.recv(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
socket.error: [Errno 11] Resource temporarily unavailable

The corresponding server:

>>> x = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> x.bind(('', 1234))
>>> x.listen(1)
>>> x.accept()
(<socket._socketobject object at 0x7f4211e997c0>, ('127.0.0.1', 39146))
History
Date User Action Args
2022-04-11 14:57:21adminsetgithub: 57186
2011-09-17 18:27:48georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg144216

resolution: works for me
2011-09-14 13:41:59Florian.Ludwigcreate