Last active
July 23, 2021 03:39
-
-
Save dasl-/d2e97baeb640e320f8cf8d73a9a3ab2c 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 time | |
import string | |
import random | |
# https://stackoverflow.com/a/10260885/627663 | |
# https://stackoverflow.com/questions/13953912/difference-between-unix-domain-stream-and-datagram-sockets | |
# Create a UDP socket | |
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) | |
server_address = './uds_socket' | |
id = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(4)) | |
sock.bind(server_address + '_' + id) | |
message = (id + ' This is the message. It will be repeated.').encode() | |
try: | |
# Send data | |
print('sending "%s"' % message) | |
sent = sock.sendto(message, server_address) | |
time.sleep(4) | |
sent = sock.sendto(message, server_address) | |
# Receive response | |
print('waiting to receive') | |
data, server = sock.recvfrom(4096) | |
print('received "%s"' % data) | |
print('waiting to receive') | |
data, server = sock.recvfrom(4096) | |
print('received "%s"' % data) | |
finally: | |
print('closing socket') | |
sock.close() | |
# import socket | |
# import sys | |
# import time | |
# import string | |
# import random | |
# # Create a UDS socket | |
# sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) | |
# # Connect the socket to the port where the server is listening | |
# server_address = './uds_socket' | |
# # print >>sys.stderr, 'connecting to %s' % server_address | |
# # try: | |
# # sock.connect(server_address) | |
# # except socket.error, msg: | |
# # print >>sys.stderr, msg | |
# # sys.exit(1) | |
# id = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(4)) | |
# message = id + ' This is the message. It will be repeated.' | |
# try: | |
# # Send data | |
# print >>sys.stderr, 'sending "%s"' % message | |
# sent = sock.sendto(message, server_address) | |
# time.sleep(4) | |
# sent = sock.sendto(message, server_address) | |
# # Receive response | |
# print >>sys.stderr, 'waiting to receive' | |
# data, server = sock.recvfrom(4096) | |
# print >>sys.stderr, 'received "%s"' % data | |
# finally: | |
# print >>sys.stderr, 'closing socket' | |
# sock.close() | |
# # try: | |
# # # Send data | |
# # message = id + ' This is the message. It will be repeated.' | |
# # print >>sys.stderr, 'sending "%s"' % message | |
# # sock.sendall(message) | |
# # time.sleep(4) | |
# # sock.sendall(message) | |
# # amount_received = 0 | |
# # amount_expected = len(message) * 2 | |
# # while amount_received < amount_expected: | |
# # data = sock.recv(16) | |
# # amount_received += len(data) | |
# # print >>sys.stderr, 'received "%s"' % data | |
# # finally: | |
# # print >>sys.stderr, '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
import socket | |
import sys | |
import time | |
# Create a TCP/IP socket | |
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) | |
# Bind the socket to the port | |
server_address = './uds_socket' | |
print('starting up on %s' % server_address) | |
sock.bind(server_address) | |
while True: | |
print('\nwaiting to receive message') | |
data, address = sock.recvfrom(4096) | |
print('received %s bytes from %s' % (len(data), address)) | |
print(data) | |
if data: | |
sent = sock.sendto(data, address) | |
print('sent %s bytes back to %s' % (sent, address)) | |
# import socket | |
# import sys | |
# import os | |
# 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 | |
# # Create a UDS socket | |
# sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) | |
# # Bind the socket to the port | |
# print >>sys.stderr, 'starting up on %s' % server_address | |
# sock.bind(server_address) | |
# while True: | |
# print >>sys.stderr, '\nwaiting to receive message' | |
# data, address = sock.recvfrom(4096) | |
# print >>sys.stderr, 'received %s bytes from %s' % (len(data), address) | |
# print >>sys.stderr, data | |
# if data: | |
# sent = sock.sendto(data, address) | |
# print >>sys.stderr, 'sent %s bytes back to %s' % (sent, address) | |
# # # Listen for incoming connections | |
# # sock.listen(10) | |
# # while True: | |
# # # Wait for a connection | |
# # print >>sys.stderr, 'waiting for a connection' | |
# # connection, client_address = sock.accept() | |
# # try: | |
# # print >>sys.stderr, 'connection from %s' % client_address | |
# # # Receive the data in small chunks and retransmit it | |
# # while True: | |
# # data = connection.recv(16) | |
# # print >>sys.stderr, 'received "%s"' % data | |
# # if data: | |
# # print >>sys.stderr, 'sending data back to the client' | |
# # connection.sendall(data) | |
# # else: | |
# # print >>sys.stderr, 'no more data from', client_address | |
# # break | |
# # finally: | |
# # # Clean up the connection | |
# # connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment