Skip to content

Instantly share code, notes, and snippets.

@dwoz
Created July 23, 2017 21:10
Show Gist options
  • Save dwoz/d61e2212c7b0248fcc2c87e8104fd1cd to your computer and use it in GitHub Desktop.
Save dwoz/d61e2212c7b0248fcc2c87e8104fd1cd to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# simple_udp_client.py
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
MESSAGE = "Hello, World!"
print("UDP target IP:".format(UDP_IP))
print("UDP target port:".format(UDP_PORT))
print("message: {}".format(MESSAGE))
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.sendto(MESSAGE.encode('ascii'), (UDP_IP, UDP_PORT))
# simple_udp_server.py
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("received message: {}".format(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment