UDP Echo Client ver 20150816
My latest UDP Echo Client is written in this article. This client has informations about destination UDP Server, but it is more better to pass the destination informations as command line arguments. Today's client is upgraded based on this perspective.
#!/usr/bin/env python import argparse import socket import time from contextlib import closing parser = argparse.ArgumentParser(description = 'Command Line Argument Parser') parser.add_argument('-host', dest = 'host', required = True) parser.add_argument('-dport', dest = 'dport', required = True) parser.add_argument('-sport', dest = 'sport') argument = vars(parser.parse_args()) host = argument['host'] dport = argument['dport'] sport = argument['sport'] sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if sport != None: sock.bind(('', int(sport))) with closing(sock): message = 'Hello!!!' sock.sendto(message, (host, int(dport))) data, server = sock.recvfrom(8192) print '%s returns %s' % server, data