Python network communication over UDP socket
Petr Faltus development
net_udp_receiver.py
GitHub repository: network-socket-communication-source-codes
git clone https://github.com/petrfaltus/network-socket-communication-source-codes.git
Directories to net_udp_receiver.py in the repository: python/src
import socket RECEIVER_ADDRESS = "0.0.0.0" RECEIVER_PORT = 10000 BUFFER_SIZE = 4096 print("UDP IPv4 datagram socket receiver") receivingSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) print("- socket created") receiver = (RECEIVER_ADDRESS, RECEIVER_PORT); receivingSocket.bind(receiver) print("- socket bound on " + RECEIVER_ADDRESS + ":" + str(RECEIVER_PORT)) stop = False while (stop == False): (buffer, peer) = receivingSocket.recvfrom(BUFFER_SIZE) received_length = len(buffer) msg = buffer.decode() (peer_address, peer_port) = peer print("- message " + str(received_length) + "B received from " + peer_address + ":" + str(peer_port) + " on port " + str(RECEIVER_PORT)) print("|" + msg + "|") if msg == "stop": stop = True receivingSocket.close() print("- socket closed")
net_udp_sender.py
Directories to net_udp_sender.py in the repository: python/src
import socket import sys from datetime import datetime RECEIVER_ADDRESS = "127.0.0.1"; RECEIVER_PORT = 10000; print("UDP IPv4 datagram socket sender") sendingSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) print("- socket created") if len(sys.argv) > 1: msg = sys.argv[1] # message is the first parameter, for example "stop" to stop the receiver else: now = datetime.now() msg = "This is Python message sent at " + now.strftime("%d.%m.%Y") + " in " + now.strftime("%H:%M:%S") print("|" + msg + "|") buffer = str.encode(msg) receiver_address = (RECEIVER_ADDRESS, RECEIVER_PORT) msg_length = len(msg) sendingSocket.sendto(buffer, receiver_address) (local_address, local_port) = sendingSocket.getsockname() print("- message " + str(msg_length) + "B sent to " + RECEIVER_ADDRESS + ":" + str(RECEIVER_PORT) + " on port " + str(local_port)) sendingSocket.close(); print("- socket closed")
Development tools
Developer ASCII table
Characters and HTML entities in the UTF-8 table
Predefined web CSS colors
CSS px to rem converter
🤝 Your IP address is 52.15.37.74
(ec2-52-15-37-74.us-east-2.compute.amazonaws.com
)