sockets and servers
Guy
guy.flowers at Machineworks.com
Tue Oct 28 10:19:40 EST 2003
Hi
Below, code that I found in a book. Server that you can open a telnet
session to.
I don't fully understand this code, but I have used it as it provided
an exstremly good way of creating what I wanted.
The trouble I'm having at the moment is that I can't find a way to
access any var in the uc_handler class (example guy)
or run functions under that class, I would like to run a function from
below the asyncore.poll(SERVER_TIMEOUT) line
example the guy function. The code I've provided is just an example,
my code is a lot longer than this and will take ages to exsplain.
I understand that each user must have a differnt instance of the
class, but I can't find out how to access from here,
or even if this is possible.
Code below :
import asyncore, asynchat, socket, sys
class uc_server(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind((host, port))
self.listen(5)
def handle_accept(self):
uc_handler(self, self.accept())
class uc_handler(asynchat.async_chat):
def __init__(self, server, (conn, addr)):
print "Connection from", addr
sys.stdout.flush()
self.addr = addr
self.server = server
self.buffer = ''
self.outbuffer = ''
self.guy="1"
asynchat.async_chat.__init__(self, conn)
self.set_terminator('\n')
def collect_incoming_data(self, data):
self.buffer += data.replace('\r', '')
def found_terminator(self):
data = self.buffer.upper()
if data == '':
print "Disconnect:", self.addr
sys.stdout.flush()
self.close()
return
self.buffer = ''
self.push(data + '\r\n')
def guy(self):
print "guy"
server = uc_server('', 9999)
while 1:
asyncore.poll(SERVER_TIMEOUT)
TIA
TTFN
Guy
More information about the Python-list
mailing list