Created
June 8, 2024 22:07
-
-
Save jweinst1/4aa9fa123b371b69b1c31d0b2c9bfc04 to your computer and use it in GitHub Desktop.
non blocking unix sockets in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import socket | |
>>> server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
>>> server.setblocking(False) | |
>>> server.bind("foo.sock") | |
>>> server.listen(1) | |
>>> connection, client_address = server.accept() | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 293, in accept | |
fd, addr = self._accept() | |
^^^^^^^^^^^^^^ | |
BlockingIOError: [Errno 35] Resource temporarily unavailable | |
>>> client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
>>> client.setblocking(False) | |
>>> client.connect("foo.sock") | |
>>> connection, client_address = server.accept() | |
>>> connection | |
<socket.socket fd=5, family=1, type=1, proto=0, laddr=foo.sock> | |
>>> client | |
<socket.socket fd=4, family=1, type=1, proto=0, raddr=foo.sock> | |
>>> client_address | |
'' | |
>>> message = 'Hello from the client!' | |
>>> client.sendall(message.encode()) | |
>>> data = connection.recv(1024) | |
>>> data | |
b'Hello from the client!' | |
>>> connection.sendall(data) | |
>>> data = client.recv(1024) | |
>>> data | |
b'Hello from the client!' | |
>>> connection.close() | |
>>> client.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment