I would you like write a client in python, that send packet to a TCP server.
I don't know the server implemantation, but it always return a menù like this (for example after nc server 4444):
Make your choice:
1- test1
2- test2
3- insert two numbers
4- test4
5- test5
6- test6
Then I want automatize all with python, for example I would send follows sequence:
1 (test1)
3 (insert two numbers)
2
1
6 (test6)
I have tried with follow script
class Connect(object):
def connect(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('connecting to host')
sock.connect(('127.0.0.1',4444))
return sock
def send(self, command):
sock = self.connect()
recv_data = ""
data = True
print('sending: ' + command)
sock.send(command)
while data:
data = sock.recv(1024)
recv_data += data
print('received: ' + data)
sock.close()
return recv_data
def main():
connect = Connect()
connect.send("1")
connect.send("3")
connect.send("2")
connect.send("1")
connect.send("6")
if __name__ == "__main__":
main()
But it does not work!
Any suggestion?
Thanks!
-
No way to know for sure without seeing the server implementation, but given the server is passing you a text menu back, it seems likely that the server expects a carriage-return or newline at the end of each command.Gil Hamilton– Gil Hamilton2016年10月26日 15:37:06 +00:00Commented Oct 26, 2016 at 15:37
-
@GilHamilton yes it is correct it need a new line at the end of each command. For example with nc it works well, but when I insert the new line for each command, only the first (connect.send("1\n")) is executed, and wait foreverLivio– Livio2016年10月27日 07:24:57 +00:00Commented Oct 27, 2016 at 7:24
2 Answers 2
Some notes:
1. Don't open and close the connection each time you use send. We don't know the server implementation, but it's absolutely clear that server won't be able to say if it is the same client has reconnected or it's a new client. TCP simply has no tools for this. Even the client port number in your case is chosen randomly by your OS. Open the connection once and close it when you're done.
2. Your are likely to stuck here forever at some point:
while data:
data = sock.recv(1024)
If the server has closed the connection, you may get an exception. If the server hasn't got anything to send, it will wait forever. Use select to check if the server has sent you anything or use settimeout + exception hadnling to make recv wait for a certain time.
A quick non-select example of sock.recv replacement of questionable quality:
def try_recv(sock, bufsize, timeout):
old_timeout = sock.gettimeout()
sock.settimeout(timeout)
try:
data = sock.recv(bufsize)
except socket.timeout:
data = None
sock.settimeout(old_timeout)
return data
1 Comment
Perhaps you don't need to use python. This bash one-liner seems to be doing what you need:
printf "1\n3\n2\n1\n6\n" | telnet 127.0.0.1 4444