Simple server using asyncore/asynchat
Rick
rrasss at gmail.com
Tue Jan 1 22:55:45 EST 2008
Hey folks. I'm trying to create a (fairly) simple server that listens for
connections and when it receives one sends a message to the client and then
waits for a response (and would continue to do that). My problem is, every
time my client connects, the server doesn't send the text and then
immediately closes the connection with the client, but DOES print that the
client was connected. Anyone know why? Here's my code:
# conn.py
import socket
import asyncore
import asynchat
import string
host = 'localhost' #socket.gethostname ()
port = 8017
buf = 1024
class ConnChannel (asynchat.async_chat):
def __init__(self, conn):
self.conn = conn
def handle_connect():
self.send(self.conn, "Hello, Welcome to BasicMUD.\r\n") # it doesn't
do thise
self.numberclients = self.numberclients + 1 # or this
self.send(self.conn, "There are " + self.numberclients + " players
online.") # or this
print "Client connected!" # it prints this
def handle_read(self):
print self.recv(8192)
def handle_write(self):
print "sending"
class ConnTube (asyncore.dispatcher):
def __init__(self, hostd, portd):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM )
self.bind ( ( hostd , portd ) )
self.listen ( 5 )
self.numberclients = 0
def handle_accept(self):
channel, addr = self.accept()
ConnChannel(channel)
connTube = ConnTube(host, port)
asyncore.loop()
Thanks everyone!
Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080101/6999b08f/attachment.html>
More information about the Python-list
mailing list