Skip to content

Instantly share code, notes, and snippets.

@geertj
Last active April 12, 2021 08:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geertj/4325783 to your computer and use it in GitHub Desktop.
Save geertj/4325783 to your computer and use it in GitHub Desktop.
Emulates the UNIX socketpair() system call on Windows. This function uses a trick with non-blocking sockets to prevent the need for a thread. A socketpair can be used as a full-duplex, select()able pipe on Windows.
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)
@mhils
Copy link

mhils commented Aug 7, 2015

FWIW, now that .socketpair() support is in Python 3.5,
I published a backports.socketpair package on PyPi.

@geertj
Copy link
Author

geertj commented Mar 19, 2016

@mhills thank you!

@Assessor3PC
Copy link

Is this gist considered public domain?

@openalmeida
Copy link

openalmeida commented Jul 21, 2020

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).

@hrieke
Copy link

hrieke commented Sep 28, 2020

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!

@ell1e
Copy link

ell1e commented Oct 30, 2020

<removed for now, will relink later>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment