Livio
Livio

Reputation: 197

Python server connection

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!

Upvotes: 0

Views: 1165

Answers (2)

u354356007
u354356007

Reputation: 3215

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

Upvotes: 0

u354356007
u354356007

Reputation: 3215

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

Upvotes: 1

Related Questions