sockets and servers

G

Guy

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
 
P

Paul Clinch

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.

I'm not sure what you're trying to do. I'll assume you want to run function guy
when the server gets the terminator, '\n' in the example. The self parameter
references all the instances data and functions.

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')
self.guy()


Regards, Paul Clinch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top