problem with async_chat

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rix

    #1

    problem with async_chat

    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.dispa tcher):

    def __init__ (self, port):
    asyncore.dispat cher.__init__ (self)
    self.create_soc ket (socket.AF_INET , socket.SOCK_STR EAM)
    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_termin ator ('\n')
    self.server = server
    self.id = self.channel_co unter
    self.channel_co unter = self.channel_co unter + 1
    self.buffer = ''
    print ("Receiver created")

    def collect_incomin g_data (self, data):
    self.buffer.app end(data)
    print ("collecting data")

    def found_terminato r (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_termi nator ("\n")
    self.create_soc ket (socket.AF_INET , socket.SOCK_STR EAM)
    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

Working...