socket troubles (repost)

David Fisher dnf1 at cec.wustl.edu
Wed Feb 16 12:05:49 EST 2000


Sorry if this is a repeat, i can't see the first post on the mailing
list, so i'm sending it again

Hey, interesting program.  I don't do much with Tk, so I learned a bit
reading this.  Too bad createfilehandler doesn't work on win32, where
I do most of my programming.  Luckily my computer dual boots :).

Anyhoo, your code was fine for send and recv on a socket, the problem
was that telnet protocol is more that just sending and receiving.  I
don't know what the protocol is, luckily I don't have to, the
telnetlib does.  And by another lucky coincidence you can pass a
Telnet object instead of a socket to the Tk handers.  Cool huh?

Your code with the telnetlib changes is below.  I also changed a
couple of other things in writesock.  I added a newline to the data
going out, and cleared the entry buffer after sending.

But.

Before you go any further, why reinvent the wheel?  Search the vaults
of parnassus (http://www.vex.net/parnassus/) for 'mud' and you might
find a few mud clients already written in Python.

BTW, on a style note, I wouldn't import all those modules using from
import *.  I'd be worried about a name collision.  But, then I'm a
slow, cautious kind of guy.

David

#!/usr/bin/python

from Tkinter import *
from _tkinter import *
from string import *
from re import *
from telnetlib import Telnet

#*************************
#*** Get input from the socket and post it to the screen
#*************************
def readsock(*args):
  data = s.read_eager()

  #*** Telnet arbitration stuff here
  if compile(chr(255)).search(data):
    print "IAC\n"

  textbox.config(state="normal")
  textbox.insert(index="insert", chars=data)
  textbox.see(index='end')
  textbox.config(state="disabled")

#*************************
#*** Write to the socket
#*************************
def writesock(*args):
  global contents
  info = contents.get() + '\n'
  contents.set('')
  s.write(info)

#*************************
#*** Declare some variables
*************************
HOST = 'shwaine.mudservices.com'
PORT = 3000

#*************************
#*** Set up what it looks like
#*************************
root = Tk()

outputscroll = Scrollbar(root)
textbox = Text(root)
inputbox = Entry(root)

outputscroll.config(command=textbox.yview)
textbox.config(yscrollcommand=outputscroll, state="disabled")

contents = StringVar()
inputbox["textvariable"] = contents

inputbox.bind(sequence="<Return>", func=writesock)

inputbox.pack(side="bottom", fill="x")
outputscroll.pack(side="right", fill="y")
textbox.pack(expand=1, fill="both")

s = Telnet(HOST, PORT)

createfilehandler(s, READABLE, readsock)

mainloop()

>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<

On 2/14/00, 12:21:57 AM, Sean Conley <sconley at cs.csubak.edu> wrote
regarding socket troubles:

> This problem has me completely stumped.  I am trying to write a telnet

> client (actually a mud client) in Python.  The problem is that I can
> connect and read information from the socket, but nothing seems to
> happen when I send to the socket.  I believe it may be a problem with
my
> program, as I had the same problem with Perl/Tk and was unable to
solve
> it there either.  So, if anyone has ANY idea why this could be please
> email me or post here.  Forgive any horrible syntax as this is my
first
> attempt at a python program and I am just trying to get the skeleton,
> and the most important part of the program working before I start
> cleaning it up.  Anyhow, here is the code:





More information about the Python-list mailing list