simple chat server

Giampaolo Rodolà g.rodola at gmail.com
Fri Jun 11 12:35:57 EDT 2010


In every dispatcher instance of your application I recommend to
override handle_error as follows:

class A(asyncore.dispatcher)

     def handle_error(self):
         raise

This will print a common traceback message instead of the compact one
provided by asyncore which provides a lot less information.


--- Giampaolo
http://code.google.com/p/pyftpdlib
http://code.google.com/p/psutil

2010/6/11 Burakk <burak.kuyucakli at gmail.com>:
> Hi,
>
> I am using ubuntu lucid and i have started to learn python(vrs 3.1). I
> am trying to make a tutorial code(see below) work but when i run the
> code, open a terminal window and connect as client with telnet and
> type somethings and hit enter, give me error below...(the terminal
> says connection closed by foreign host)
>
> if someone can help i will be glad...
>
> thanx
>
> -- error: uncaptured python exception, closing channel
> <__main__.ChatSession connected 127.0.0.1:46654 at 0xb71cce8c> (<class
> 'TypeError'>:expected an object with the buffer interface [/usr/lib/
> python3.1/asyncore.py|read|75] [/usr/lib/python3.1/asyncore.py|
> handle_read_event|420] [/usr/lib/python3.1/asynchat.py|handle_read|
> 170]) --
>
> code
>
> from asyncore import dispatcher
> from asynchat import async_chat
> import asyncore
> import socket
>
> PORT = 5005
> NAME = 'TestChat'
>
> class ChatSession(async_chat):
>
>     def __init__(self, sock):
>         async_chat.__init__(self, sock)
>         self.set_terminator("xx")
>         self.data = []
>
>
>     def collect_incoming_data(self, data):
>         self.data.append(data)
>
>
>     def found_terminator(self):
>         line = ''.join(self.data)
>         self.data = []
>         self.push(line)
>
> class ChatServer(dispatcher):
>      def __init__(self, port):
>          dispatcher.__init__(self)
>          self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
>          self.set_reuse_addr()
>          self.bind(('', PORT))
>          self.listen(5)
>          self.sessions = []
>
>
>      def handle_accept(self):
>          conn, addr = self.accept()
>          self.sessions.append(ChatSession(conn))
>
> if __name__== '__main__':
>    s = ChatServer(PORT)
>    try: asyncore.loop()
>    except KeyboardInterrupt : print
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list