simple chat server

B

Burakk

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
 
P

Peter Pearson

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...

I know this is very little help, but your code appears to
work fine under Python 2.5.2.
 
B

Burakk

Thank you for your replies...

I have overriden the error handlers
and the results:

Traceback (most recent call last):
File "/home/burak/NetBeansProjects/intantMarkup/src/
02basicServer.py", line 65, in <module>
try: asyncore.loop()
File "/usr/lib/python3.1/asyncore.py", line 206, in loop
poll_fun(timeout, map)
File "/usr/lib/python3.1/asyncore.py", line 147, in poll
read(obj)
File "/usr/lib/python3.1/asyncore.py", line 79, in read
obj.handle_error()
File "/usr/lib/python3.1/asyncore.py", line 75, in read
obj.handle_read_event()
File "/usr/lib/python3.1/asyncore.py", line 420, in
handle_read_event
self.handle_read()
File "/usr/lib/python3.1/asynchat.py", line 170, in handle_read
index = self.ac_in_buffer.find(terminator)
TypeError: expected an object with the buffer interface


But i want to add that i am writing the code in netbeans with python
3.1 and running the code from the programme.. Ubuntu deafult python is
2.6.5. Is this a problem. And i also want to add that i made the .py
executable and run that from terminal too with the same
results...
 
T

Thomas Jollans

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]) --

my guess is that you can't use (unicode) strings as network data. You'd
have to use byte strings most of the time. (only a guess)
This code was probably written with Python 2 in mind.

See below
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")

replace that with b"xx"
self.data = []


def collect_incoming_data(self, data):
self.data.append(data)


def found_terminator(self):
line = ''.join(self.data)

use b''.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

This won't do anything: print is now a function, not a statement. Use
print() instead.

Also, that's silly. If you want to ignore keyboard interrupts (I don't
think that's a good idea), use "pass" instead of "print()".
 

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,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top