socket troubles

Sean Conley sconley at cs.csubak.edu
Mon Feb 14 01:21:57 EST 2000


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: 

#!/usr/bin/python

from Tkinter import *
from _tkinter import *
from socket import *
from string import *
from re import *

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

  #*** 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()
  s.send(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 = socket(AF_INET, SOCK_STREAM)
s.connect(HOST, PORT)

createfilehandler(s, READABLE, readsock)

mainloop()



More information about the Python-list mailing list