Skip to content

Instantly share code, notes, and snippets.

@acg
Created December 5, 2012 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acg/4211098 to your computer and use it in GitHub Desktop.
Save acg/4211098 to your computer and use it in GitHub Desktop.
Factoring out the listen(2) half of djb's tcpserver.
#!/usr/bin/env python
import sys
import os
import socket
port = sys.argv.pop(0)
host = sys.argv.pop(0)
port = int(sys.argv.pop(0))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(32)
os.dup2(s.fileno(),0)
os.close(s.fileno())
os.execvp(sys.argv[0],sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment