Socket connection to telnet server freezes while reading

I am trying to write a simple socket client in Python that will connect to a telnet server. I can check the server by telnetting to its port (5007) and enter the text. He responds with NAK (error) or AK (success), sometimes accompanied by another text. It seems very simple.

I wrote a client to connect and communicate with the server, but it freezes on the first attempt to read the answer. Connection completed successfully. Requests such as getsockname and getpeername succeeded. The send command returns a value equal to the number of characters I send, so it seems like they are sent correctly. But in the end, it always freezes when I try to read the answer.

I tried using both file objects such as readline and write (via socket.makefile), and also use send and recv. With the file, I tried to make it using "rw" and reading and writing through this object, and then I tried one object for "r" and another for "w" to separate them. None of them worked.

I used a packet analyzer to see what happens. I do not understand everything that I see, but during a telnet session I see that my typed text and server text are being returned. During my connection to the Python socket, I can see that my text goes to the server, but the packets back do not seem to have any text in them.

Any ideas on what I'm doing wrong, or any strategies to try?

Here is the code I'm using (in this case, it is with send and recv):

#!/usr/bin/python

host = "localhost"
port = 5007
msg = "HELLO EMC 1 1"
msg2 = "HELLO"

import socket
import sys

try:
    skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, e:
    print("Error creating socket: %s" % e)
    sys.exit(1)

try:
    skt.connect((host,port))
except socket.gaierror, e:
    print("Address-related error connecting to server: %s" % e)
    sys.exit(1)
except socket.error, e:
    print("Error connecting to socket: %s" % e)
    sys.exit(1)

try:
    print(skt.send(msg))
    print("SEND: %s" % msg)
except socket.error, e:
    print("Error sending data: %s" % e)
    sys.exit(1)


while 1:
    try:
        buf = skt.recv(1024)
        print("RECV: %s" % buf)
    except socket.error, e:
        print("Error receiving data: %s" % e)
        sys.exit(1)
    if not len(buf):
        break
    sys.stdout.write(buf)

, , telnet, :

ubuntu:~/mac/python$ telnet localhost 5007
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
HELLO EMC 1 1
HELLO ACK EMCNETSVR 1.1

"HELLO" - , , - .

+5
2

, msg - " " - , \r\n, , . telnet, , Return Enter? Python .

+7

flush send, TCP- . , . , .

0

All Articles