Created
January 27, 2014 12:19
-
-
Save KentaKomai/8647600 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding:utf-8 -*- | |
import socket | |
import sys | |
version = 1.0 | |
email = "kaede06152000@gmail.com" | |
host = 'localhost' | |
port = 1234 | |
sendMessage = 'send by TcpSender' | |
count = 1 | |
def main(): | |
printCopyRight() | |
argvs = checkNumberOfInput(sys.argv) | |
try: | |
if isinstance(argvs[1], str): | |
host = argvs[1] | |
if isinstance(int(argvs[2]), int): | |
port = int(argvs[2]) | |
if isinstance(argvs[3], str): | |
sendMessage = argvs[3] | |
if isinstance(int(argvs[4]), int): | |
count = int(argvs[4]) | |
printSendPacketInformation(host, port, sendMessage, count) | |
except: | |
printAbnormalTermination() | |
return | |
try: | |
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
clientSocket.connect( (host, port ) ) | |
except socket.error,(errno, string): | |
print "Error Message > ", string | |
printAbnormalTermination() | |
exit() | |
for i in range(0, count): | |
clientSocket.sendall(sendMessage) | |
print "send..." | |
rcvmsg = clientSocket.recv(1024) | |
if rcvmsg != '': | |
print "Received -> %s" % (rcvmsg) | |
else: | |
print "Received no message." | |
while True: | |
isContinue = raw_input("continue ? Y/N >") | |
if isContinue.upper() == 'N': | |
break | |
elif isContinue.upper() != 'Y': | |
continue | |
sendMessage = raw_input("please input sending message > ") | |
if sendMessage = '': | |
break | |
clientSocket.sendall(sendMessage) | |
print "send..." | |
rcvmsg = clientSocket.recv(1024) | |
if rcvmsg != '': | |
print "Received -> %s" % (rcvmsg) | |
else: | |
print "Received no message." | |
clientSocket.close() | |
print "connection closed. Application is complete." | |
exit() | |
def checkNumberOfInput( argvs ): | |
if(len(argvs) < 5): | |
printUsage() | |
exit() | |
return argvs | |
def printSendPacketInformation(host, port, sendMessage, count): | |
print "----------------------------------------------------------------" | |
print "Packet Information" | |
print "host:", host | |
print "port:", port | |
print "message:", sendMessage | |
print "number of time:", count | |
print "----------------------------------------------------------------" | |
def printAbnormalTermination(): | |
print "Sorry, Abnormality occurred and was finished..." | |
print "Please checking input and error message." | |
def printCopyRight(): | |
print "----------------------------------------------------------------" | |
print "TcpSender ver.", str(version) | |
print "" | |
print "Auther: Kenta Komai" | |
print "Contact:", email | |
print "----------------------------------------------------------------" | |
def printUsage(): | |
print "Usage: ./TcpSender <IP-Address> <PortNumber> <Message> <number of time>" | |
# Entry point | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment