0

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!

asked Oct 26, 2016 at 13:52
2
  • 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. Commented 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 forever Commented Oct 27, 2016 at 7:24

2 Answers 2

1

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
answered Oct 27, 2016 at 9:09
Sign up to request clarification or add additional context in comments.

1 Comment

Point 2 could be my solution, I have to test it and then I will give you a feedback
0

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
answered Oct 27, 2016 at 9:19

1 Comment

It works(also with nc), but I need to send also hex numbers (like "\x40\x41\x00\x80")

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.