Skip to content

Instantly share code, notes, and snippets.

@mattmahn
Created September 16, 2017 23:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmahn/d5006cb7f1ff8498ab54aa065a2fb31f to your computer and use it in GitHub Desktop.
Save mattmahn/d5006cb7f1ff8498ab54aa065a2fb31f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version π, September 2017
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
import socket
if __name__ == '__main__':
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
sock.connect('/var/run/example.sock')
data = bytes(input('-> ') + '\n', 'utf-8')
sock.send(data)
length = len(data)
received = str(sock.recv(length), 'utf-8').strip()
print(received)
sock.shutdown(socket.SHUT_RDWR)
sock.close()
#!/usr/bin/env python3
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version π, September 2017
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
import socketserver
class Handler(socketserver.StreamRequestHandler):
def handle(self):
# self.rfile is a file-like object created by the handler;
# we can now use e.g. readline() instead of raw recv() calls
self.data = self.rfile.readline().strip()
print(self.data)
# Likewise, self.wfile is a file-like object used to write back
# to the client
self.wfile.write(self.data.swapcase())
if __name__ == '__main__':
with socketserver.UnixStreamServer('/var/run/example.sock', Handler) as srv:
print('Listening...')
srv.serve_forever()
print('done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment