473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

sockets and servers

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
Jul 18 '05 #1
1 1530
gu*********@Machineworks.com (Guy) wrote in message news:<f3**************************@posting.google. com>...
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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jblazi | last post by:
I am having this problem: I use Python for my teaching and right now we are trying to implement some sort of very simple graphical game the pupils can play across the Internet. For this, we use...
1
by: Dfenestr8 | last post by:
Hi. I realise, that there's probably something intrinsic to OOP that I don't understand here. Or maybe it's something to do with sockets. I'm not sure. Basically, I'm trying to hack up an IRC...
4
by: crashnburn | last post by:
Hi there, I was wondering if C++ has some kind of default new socket wrapper, that avoids many lines of code. Maybe there is a good C++ library? Any ideas? I'm using UNIX, but portability...
5
by: Virgil Green | last post by:
An associate has asked me to take a look a problem he's having with starting mysql. I thought I'd give it a shot to learn some more about mySQL before having him turn it over to someone more...
10
by: Cory Nelson | last post by:
I've created a new C++ sockets library - small, cross-platform, IPv4 and IPv6. If anyone would like to critique it, I'd appreciate it. Doesn't support the more exotic protocols - only TCP and UDP....
1
by: Klaus | last post by:
Hello, I did post this message already recently, but do have the impression, that this message never has never been pushed to other news servers. Well.
5
by: Jason L James | last post by:
Hi all, as a VB.Net programmer I am finding it difficult to find a text on implementing sockets to communicate with an embedded web server. Does anyone know of a good resource, either text...
2
by: abhi147 | last post by:
Hi , I have a program which has 1 client and 4 servers . Client sends messages(every second) to all these 4 servers and recieves a response from these servers . It has load balaning and failover...
4
by: zaferaydin | last post by:
Hi, i have a Socket based application that works well. The application connects remote servers and getting data from them. When i try to read data from 20-30 servers at the same time, i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.