Skip to content

Instantly share code, notes, and snippets.

@Jiang1155
Forked from jmhobbs/client.py
Last active February 12, 2021 19:00
Show Gist options
  • Save Jiang1155/99e229c888510f65d9ed8f8e7d0cfc6e to your computer and use it in GitHub Desktop.
Save Jiang1155/99e229c888510f65d9ed8f8e7d0cfc6e to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import socket
import os
#don't connect, just send, see what error we will get.
print("Starting...")
client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
print("Ready.")
print("Ctrl-C to quit.")
print("Sending 'DONE' shuts down the server and quits.")
while True:
try:
x = input("> ")
if "" != x:
print("SEND:", x)
client.sendto(x.encode('utf-8'),"/tmp/python_unix_sockets_example")
if "DONE" == x:
print("Shutting down.")
break
except KeyboardInterrupt as k:
print("Shutting down.")
client.close()
break
print("Done")
# -*- coding: utf-8 -*-
import socket
import os
if os.path.exists("/tmp/python_unix_sockets_example"):
os.remove("/tmp/python_unix_sockets_example")
print("Opening socket...")
server = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
server.bind("/tmp/python_unix_sockets_example")
print("Listening...")
while True:
datagram = server.recv(1024)
if not datagram:
break
else:
print("-" * 20)
print(datagram.decode('utf-8'))
if "DONE" == datagram.decode('utf-8'):
break
print("-" * 20)
print("Shutting down...")
server.close()
os.remove("/tmp/python_unix_sockets_example")
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment