Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket connection to a telnet-based server hangs on read

Tags:

python

sockets

I'm trying to write a simple socket-based client in Python that will connect to a telnet server. I can test the server by telnetting to its port (5007), and entering text. It responds with a NAK (error) or an AK (success), sometimes accompanied by other text. Seems very simple.

I wrote a client to connect and communicate with the server, but it hangs on the first attempt to read the response. The connection is successful. Queries like getsockname and getpeername are successful. The send command returns a value that equals the number of characters I'm sending, so it seems to be sending correctly. But in the end, it always hangs when I try to read the response.

I've tried using both file-based objects like readline and write (via socket.makefile), as well as using send and recv. With the file object I tried making it with "rw" and reading and writing via that object, and later tried one object for "r" and another for "w" to separate them. None of these worked.

I used a packet sniffer to watch what's going on. I'm not versed in all that I'm seeing, but during a telnet session I can see my typed text and the server's text coming back. During my Python socket connection, I can see my text going to the server, but packets back don't seem to have any text in them.

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

Here's the code I'm using (in this case, it's 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)

Oh, in case it's useful, here's an example telnet session done manually:

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

The first 'HELLO' line is what I typed, the second one is the response.

like image 514
mix Avatar asked May 23 '10 21:05

mix


1 Answers

You probably need to terminate your msg with some kind of "line-ending characters" -- perhaps \r\n, perhaps just one of the two. When you're in telnet, didn't you terminate your typed text by hitting a Return or Enter key? In the Python code, you're not doing the equivalent of that.

like image 53
Alex Martelli Avatar answered Sep 28 '22 06:09

Alex Martelli