Skip to content

Instantly share code, notes, and snippets.

@thobroni
Created May 12, 2017 23:01
Show Gist options
  • Select an option

  • Save thobroni/b4b35acf0ce9e404056a79068d23e011 to your computer and use it in GitHub Desktop.

Select an option

Save thobroni/b4b35acf0ce9e404056a79068d23e011 to your computer and use it in GitHub Desktop.
UDP client
import socket
import time
import sys
def main():
argc = len(sys.argv)
addr = "127.0.0.1"
PORT = 801
if(argc > 1):
addr = sys.argv[1]
if(argc > 2):
PORT = sys.argv[2]
print(addr)
HOST = socket.gethostbyname(addr)
print("Will send to {0}:{1}".format(HOST, PORT))
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
try:
print("Sending")
sock.sendto("Hi there".encode("UTF-8"), (HOST, PORT))
time.sleep(1)
except socket.error as msg:
sock.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment