Skip to content

Instantly share code, notes, and snippets.

@geertj
Last active October 3, 2024 16:11
Show Gist options
  • Save geertj/4325783 to your computer and use it in GitHub Desktop.
Save geertj/4325783 to your computer and use it in GitHub Desktop.

Revisions

  1. geertj renamed this gist Jan 17, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. geertj created this gist Dec 18, 2012.
    19 changes: 19 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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)