Here is a complete example that has been tested with Python 2.7.5 on CentOS 7.
#!/usr/bin/python
import sys, socket
def main(args):
ip = args[1]
port = int(args[2])
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
file = 'sample.csv'
fp = open(file, 'r')
for line in fp:
sock.sendto(line.encode('utf-8'), (ip, port))
fp.close()
main(sys.argv)
The program reads a file, sample.csv
from the current directory and sends each line in a separate UDP packet. If the program it were saved in a file named send-udp
then one could run it by doing something like:
$ python send-udp 192.168.1.2 30088