[Tutor] A request
Ali Polatel
alipolatel at yahoo.com
Sat Sep 25 10:45:06 CEST 2004
Dear Python Programmers,
I have a request from a Python programmer who can do me a favor...
This year I'll graduate from my high-school...
I'll host a chat-server so that we will be able to chat as all friends will leave the city to go to different universities...
I was looking around if there is one written in Python...I found this below:
"
# A simple "chat" server. Creates a server on port 4000.# Users telnet to your machine, port 4000, and can chat with each# other. Use "quit" to disconnect yourself, and "shutdown" to# shut down the server. Requires sockets.## 7/31/96 J. Strout http://www.strout.net/# import needed modules:from socket import * # get sockets, for well, socketsimport string # string functionsimport time # for sleep(1) function# define global variablesHOST = '' # Symbolic name meaning the local hostPORT = 4000 # Arbitrary non-privileged serverendl = "\r\n" # standard terminal line endinguserlist = [] # list of connected usersdone = 0 # set to 1 to shut this downkAskName = 0 # some constants used to flagkWaitName = 1 # the state of each userkOK = 2# class to store info about connected usersclass User: def __init__(self): self.name = "" self.addr = "" self.conn = None self.step = kAskName def Idle(self): if self.step == kAskName: self.AskName() def AskName(self):
self.conn.send("Name? ") self.step = kWaitName def HandleMsg(self, msg): print "Handling message: ",msg global userlist # if waiting for name, record it if self.step == kWaitName: # try to trap garb initiall sent by some telnets: if len(msg) < 2 or msg=="#": return print "Setting name to: ",msg self.name = msg self.step = kOK self.conn.send("Hello, "+self.name+endl) broadcast(self.name+" has connected."+endl) return # check for commands if msg == "quit": broadcast(self.name+" has quit.\n") self.conn.close() userlist.remove(self) return # otherwise, broadcast msg broadcast( self.name+": "+msg+endl )# routine to check for incoming connectionsdef pollNewConn(): try: conn, addr = s.accept() except: return None print "Connection from", addr conn.setblocking(0) user = User(); user.conn = conn user.addr = addr return user# routine to broadcast a message to all connected usersdef broadcast(msg): for u in userlist: u.conn.send(msg)# MAIN PROGRAM# set up
the servers = socket(AF_INET, SOCK_STREAM)s.bind(HOST, PORT)s.setblocking(0)s.listen(1)print "Waiting for connection(s)..."# loop until done, handling connections and incoming messageswhile not done: time.sleep(1) # sleep to reduce processor usage u = pollNewConn() # check for incoming connections if u: userlist.append(u) print len(userlist),"connection(s)" for u in userlist: # check all connected users u.Idle() try: data = u.conn.recv(1024) data = filter(lambda x: x>=' ' and x<='z', data) data = string.strip(data) if data: print "From",u.name,': ['+data+']' u.HandleMsg(data) if data == "shutdown": done=1 except: passfor u in userlist: u.conn.close()"
But this is too primitive to be a chat server I believe...
I have really no time to make it better...
Can any of you add it some functions?
like adding admin functions,and other chat functions that you can dream of
and make it similar to an IRC channel...
I'll appreciate any kind of help...
Regards and thanks all,
Ali Polatel
---------------------------------
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040925/12313657/attachment.htm
More information about the Tutor
mailing list