simple_send_packet.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from socket import *
00004 import sys
00005 import argparse
00006 import time
00007 import struct
00008 parser = argparse.ArgumentParser(description='Simple socket client')
00009 parser.add_argument("--port", default=8080, type=int)
00010 parser.add_argument("--buffer-size", default=1024, type=int)
00011 parser.add_argument("--size", default=256, type=int)
00012 parser.add_argument("--rate", default=1, type=float)
00013 parser.add_argument("--udp", action="store_true")
00014 parser.add_argument("--ip", default="127.0.0.1")
00015 args = parser.parse_args()
00016 
00017 print "connecting to ", (args.ip, args.port)
00018 if args.udp:
00019     server = socket(AF_INET, SOCK_DGRAM)
00020 else:
00021     server = socket(AF_INET, SOCK_STREAM)
00022     server.connect((args.ip, args.port))
00023 counter = 1
00024 while True:
00025     packer = struct.Struct("!%ds" % args.size)
00026     #packer = struct.Struct("!%ds" % size)
00027     data = packer.pack(("%d" % (counter % 10)) * args.size)
00028     if not data:
00029         break
00030     print "sending", packer.size * 8, "bits"
00031     if args.udp:
00032         server.sendto(data, (args.ip, args.port))
00033     else:
00034         server.send(data)
00035     if not data:
00036         break
00037     print data
00038     counter = counter + 1
00039     time.sleep(1 / args.rate)
00040 
00041 server.close()


mini_maxwell
Author(s): Yusuke Furuta
autogenerated on Sun Jan 25 2015 12:37:43