telnetlib - confused
Eddie Corns
eddie at holyrood.ed.ac.uk
Mon Aug 12 13:41:05 EDT 2002
Rich Daley <daley at ignore-this.cs.man.ac.uk> writes:
>I am pretty new to python, so please treat me like a newbie :)
>I am very keen on telnet-based chatrooms (eg <telnet:surfers.org:4242>)
>and I am interested in writing a python client to talk in these.
>My problem is that I've been trying to work out how to do something
>straightforward in telnetlib and haven't succeeded yet.
>As you can imagine, I would like an interactive session with this
>chatroom, but I don't want to use Telnet.interactive() because I don't want the
>user to be able to enter text directly, more just have it entered by
>events from the code (ie Telnet.write() events).
>However, I would like all the text that appears in the session, whether
>EOF has been reached or not to be output (in the console for now).
>Could anyone help me with this, or at least make sense of what I'm saying,
>because when I look back at it I barely understand it myself.
I *think* from what you're saying you want to use something like mt_interact.
I only noticed this myself when reviewing the docs. Here's a quick and crude
bit of demo code that replaces mt_interact with your own version:
import telnetlib as tl
import sys
rude_words = ['perl', 'microsoft']
class myTelnet (tl.Telnet):
def my_interact (self):
"""Multithreaded version of interact()."""
import thread
thread.start_new_thread(self.listener, ())
while 1:
line = sys.stdin.readline()
if not line:
break
for rw in rude_words:
if line.find (rw) != -1:
print "Tsk tsk you know you can't say things like that in public"
break
else:
self.write(line)
con = myTelnet (sys.argv[1])
con.my_interact()
This was cribbed directly from the source. Obviously what you could do here
is replace the while loop with your own checking.
Eddie
More information about the Python-list
mailing list