-
-
Save geertj/4325783 to your computer and use it in GitHub Desktop.
def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): | |
"""Emulate the Unix socketpair() function on Windows.""" | |
# We create a connected TCP socket. Note the trick with setblocking(0) | |
# that prevents us from having to create a thread. | |
lsock = socket.socket(family, type, proto) | |
lsock.bind(('localhost', 0)) | |
lsock.listen(1) | |
addr, port = lsock.getsockname() | |
csock = socket.socket(family, type, proto) | |
csock.setblocking(0) | |
try: | |
csock.connect((addr, port)) | |
except socket.error, e: | |
if e.errno != errno.WSAEWOULDBLOCK: | |
raise | |
ssock, addr = lsock.accept() | |
csock.setblocking(1) | |
lsock.close() | |
return (ssock, csock) |
@mhills thank you!
Is this gist considered public domain?
Hi,
I am come from Python's socket module of a similar function socketpair()
to this one.
Which has a URL refer to here.
Would anyone tell me, why socketpair requires a socket status back to blocked.
Namely, What the setblocking(1)
at the last is used for ?
As I can understand the Event Loop of Python Asyncio is socketpair (with select/epoll/...) based,
but Python Asyncio works fine (nonblocked), thus confused me about setblocking(1)
.
Hello,
Could you please supply a license for your code?
(It's found in Python's socket.py,
https://github.com/python/cpython/blob/master/Lib/socketserver.py
which is why I am asking)
Thanks!
<removed for now, will relink later>
Since the problem is public now, the above gist has a security issue in that with the right timing, an attacker can take over the connection: https://www.cve.org/CVERecord?id=CVE-2024-3219 Therefore, I would advise against using it. My apologies that it took so long for this to become public. The fix is to verify that the connection obtained is actually the corresponding one that was meant to connect, like done here: python/cpython#122134
FWIW, now that
.socketpair()
support is in Python 3.5,I published a
backports.socketpair
package on PyPi.