Skip to content

Instantly share code, notes, and snippets.

@robbmanes
Created May 14, 2019 20:59
Show Gist options
  • Select an option

  • Save robbmanes/a558357fc5a227132b9fa3af84eb25e8 to your computer and use it in GitHub Desktop.

Select an option

Save robbmanes/a558357fc5a227132b9fa3af84eb25e8 to your computer and use it in GitHub Desktop.
A very simple UNIX socket test application
import os
import sys
import socket
def main():
# check if directory exists, if it doesn't, make it:
if not os.path.exists('/var/run'):
os.mkdir('/var/run', 0777)
# make the socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.bind('/var/run/mysock.sock')
# listen on the socket and accept connections
sock.listen(1)
while True:
print >>sys.stdout, 'listening for connections on /var/run/mysock.sock'
conn, client = sock.accept()
try:
buf = conn.recv(256)
print >>sys.stdout, 'received data "%s" from "%s"' % (buf, client)
conn.close()
except:
print >>sys.stdout, "Failed to accept connection! Retrying..."
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment