socket troubles

Sandra Plahl sandra.plahl at desys.de
Mon Feb 14 02:52:02 EST 2000


Sean Conley wrote:
> 
> 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()

I wonder how you could ever read from the socket when you never could
send???

I don't know if you have one but thats what you need is s second
application (server), which is reading your send and reply on it. The
answer then can be read from your first application (client). In the
server try something like this:
(its a socket skeleton from a daemon process)

import socket, sys, errno
.....
.....

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
serversocket.bind((HOST, PORT))
serversocket.listen(5)
while(1):
    clientsocket = None
    done = 0
    while not done:
	try:
	    (clientsocket, address) = serversocket.accept()
	except socket.error, args:
	    if args[0] != errno.EINTR:
		raise
	else:
	    done = 1

    cpid = os.fork()
    if not cpid:
        serversocket.close()
	data = clientsocket.recv(80)
    	# Here you can do what you want with the send data 
	# Maybe send it back       
        clientsocket.send(data)	
        clientsocket.close()
    	data = None
	sys.exit(0)
    else:
        clientsocket.close()


Sandra



More information about the Python-list mailing list