By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

problem with async_chat

P: n/a
rix
Hi there,
I am writing an application that requires a client-server interaction.
Looking up on the internet and working on it I came up with something
like this:
a class ADFSServer that takes inbound connections and dispatches them
to ADFSReceiver.
The connection is always initialized by ADFSSender.
here is the code:

import asynchat
import asyncore
import socket
import string

class ADFSServer (asyncore.dispatcher):

def __init__ (self, port):
asyncore.dispatcher.__init__ (self)
self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
here = ('', port)
self.bind (here)
self.listen (1)

def handle_accept (self):
print ("Server is accepting connection")
ADFSReceiver (self, self.accept())

class ADFSReceiver (asynchat.async_chat):

channel_counter = 0

def __init__ (self, server, (conn, addr)):
asynchat.async_chat.__init__ (self, conn)
self.set_terminator ('\n')
self.server = server
self.id = self.channel_counter
self.channel_counter = self.channel_counter + 1
self.buffer = ''
print ("Receiver created")

def collect_incoming_data (self, data):
self.buffer.append(data)
print ("collecting data")

def found_terminator (self):
data = self.buffer
self.buffer = ''
print '[Received Message]\n %s' % (self.id, repr(data))
self.close()
return data

def handle_close (self):
print 'Closing receiver'
self.close()

class ADFSSender (asynchat.async_chat):

def __init__ (self, data, address):
print "creating sender"
asynchat.async_chat.__init__ (self)
#self.set_terminator ("\n")
self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
self.connect (address)
self.push(data)
self.push ("\n")

if __name__ == '__main__':
work1 = ADFSServer (1234)
asyncore.loop()
I start the server on a shell, and from another shell I launch the
ADFSSender.
The problem is that if I use the push() function the code doesn't work.
If I use the send() it works.
As I am using the async_chat class, I would like to use the push()
method, so I was wondering what was the problem with my code...

thanks,
rix

Nov 3 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.