Created
August 25, 2020 01:04
-
-
Save dasl-/90ff02273aa11416e85117eba2ecb05e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import sys | |
import traceback | |
import datetime | |
import pytz | |
def log(msg): | |
msg = (datetime.datetime.now(pytz.timezone('UTC')).isoformat() + " " + msg) | |
print(msg, file = sys.stdout, flush = True) | |
# Create a UDS socket | |
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
# Connect the socket to the port where the server is listening | |
server_address = './uds_socket' | |
log('connecting to {}'.format(server_address)) | |
try: | |
sock.connect(server_address) | |
except Exception as e: | |
log('Exception: {}'.format(traceback.format_exc())) | |
sys.exit(1) | |
sock.settimeout(3) | |
try: | |
# Send data | |
message = 'This is the message. It will be repeated.' | |
log('sending "{}"'.format(message)) | |
sock.sendall(message.encode()) | |
log("done sending") | |
amount_received = 0 | |
amount_expected = len(message) | |
while amount_received < amount_expected: | |
data = None | |
try: | |
data = sock.recv(16) | |
except Exception as e: | |
log("failed to recv") | |
raise e | |
amount_received += len(data) | |
log('received "{}"'.format(data)) | |
finally: | |
log('closing socket') | |
sock.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 client.py | |
2020-08-25T01:03:07.759512+00:00 connecting to ./uds_socket | |
2020-08-25T01:03:07.759824+00:00 sending "This is the message. It will be repeated." | |
2020-08-25T01:03:07.759989+00:00 done sending | |
2020-08-25T01:03:10.763186+00:00 failed to recv | |
2020-08-25T01:03:10.763395+00:00 closing socket | |
Traceback (most recent call last): | |
File "client.py", line 40, in <module> | |
raise e | |
File "client.py", line 37, in <module> | |
data = sock.recv(16) | |
socket.timeout: timed out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import sys | |
import os | |
import select | |
import time | |
import datetime | |
import pytz | |
def log(msg): | |
msg = (datetime.datetime.now(pytz.timezone('UTC')).isoformat() + " " + msg) | |
print(msg, file = sys.stdout, flush = True) | |
server_address = './uds_socket' | |
# Make sure the socket does not already exist | |
try: | |
os.unlink(server_address) | |
except OSError: | |
if os.path.exists(server_address): | |
raise | |
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
# Bind the socket to the port | |
log("starting up on {}".format(server_address)) | |
sock.bind(server_address) | |
sock.settimeout(3) | |
sock.listen(10) | |
log("listening") | |
time.sleep(30) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python3 server.py | |
2020-08-25T01:03:05.587219+00:00 starting up on ./uds_socket | |
2020-08-25T01:03:05.587611+00:00 listening |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment