# =========================================================
# send data
# From: pymotw.com/2/socket/tcp.html
# =========================================================
import socket as sk
import time
# create a TCP/IP socket
sock = sk.socket(sk.AF_INET, sk.SOCK_DGRAM)
# UDP server address and port
addr = 'localhost'
port = 10000
saddress = (addr,port)
print('UDP server addr {}'.format(saddress))
# send data to UDP server
i = 0
try:
while True:
msg = 'my message #{}'.format(i)
print('msg len = {}'.format(len(msg)))
print('sending message "{}"'.format(msg))
sock.sendto(msg,saddress)
if i > 4:
sock.sendto('',saddress)
break
i += 1
time.sleep(1) # pause 1 second between messages
finally: # no matter what, close the socket
# close the socket
print('closing socket')
sock.close()