Simple python telnet client/server example doesn't work
<p>I'm trying to create mockup telnet server (for some functional testing of existing code). Right now I only modified server welcome message and I was trying to read this message using client code, but it read method fails on timeout with no additional info. But with pudb debugger enabled it works most of the time...</p>

<p>I'm using virtualenv, with pudb and telnetsrv installed using pip. Python 2.7.12, ubuntu 16.04.</p>

<p>Server code:</p>

<pre><code>import SocketServer
from telnetsrv.threaded import TelnetHandler

class MyHandler(TelnetHandler):

    WELCOME = "HI from custom server"

class TelnetServer(SocketServer.TCPServer):
    allow_reuse_address = True

server = TelnetServer(("0.0.0.0", 8023), MyHandler)
server.serve_forever()
</code></pre>

<p>Client code:</p>

<pre><code>import telnetlib

HOST = '127.0.0.1'
PORT = 8023
# from pudb import set_trace; set_trace()
tn = telnetlib.Telnet(HOST, PORT)

data = tn.read_until("custom server", timeout=1)
print "Data: " + data

tn.close()
</code></pre>

<p>Client output:</p>

<pre><code>$ python client.py
Data:
</code></pre>

<p>Client output with pudb enabled (with step-by-step execution)</p>

<pre><code>$ python client.py
Data: HI from custom server
</code></pre>

<p>Of course when I execute shell telnet command, it all works fine:</p>

<pre><code>$ telnet 127.0.0.1 8023
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
HI from custom server
Telnet Server&gt; 
</code></pre>

<p>I'd really appreciate any hints on how to debug this problem. Thanks!</p>

<p>Be sure there is actually connecting going on. To do that put edit your code to add <strong>tn.set_debuglevel(100)</strong> into your script to look like this:</p>

<pre><code>import telnetlib

HOST = '127.0.0.1'
PORT = 8023
# from pudb import set_trace; set_trace()
tn = telnetlib.Telnet(HOST, PORT)
tn.set_debuglevel(100)
data = tn.read_until("custom server", timeout=1)
print "Data: " + data

tn.close()
</code></pre>

<p>This will ensure all the data is printed out so you can see what's going on.</p>

<p>My theory is, that you're not connecting, or that your data isn't actually outputting "custom server" and therefor it won't catch it, or your timeout is too low.</p>

python telnetlib