simple_echo_client.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 struct
00007 parser = argparse.ArgumentParser(description='Simple socket client')
00008 parser.add_argument("--port", default=8080, type=int)
00009 parser.add_argument("--udp", action="store_true")
00010 parser.add_argument("--ip", default="127.0.0.1")
00011 args = parser.parse_args()
00012 
00013 print "connecting to ", (args.ip, args.port)
00014 
00015 if not args.udp:
00016     server = socket(AF_INET, SOCK_STREAM)
00017     server.connect((args.ip, args.port))
00018 else:
00019     server = socket(AF_INET, SOCK_DGRAM)
00020     while True:
00021         data = raw_input('> ')
00022         if not data:
00023             break
00024         packer = struct.Struct("!%ds" % len(data))
00025         data = packer.pack(data)
00026         print "sending", packer.size * 8, "bits"
00027         if not args.udp:
00028             server.send(data)
00029         else:
00030             server.sendto(data, (args.ip, args.port))
00031         if not data:
00032             break
00033         print data
00034     server.close()


mini_maxwell
Author(s): Yusuke Furuta
autogenerated on Sat Sep 9 2017 02:33:35