<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2668" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hello,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>I am a Java Developer that has been learning Python 
by doing simple things. I am loving this initial vibe I'm getting 
out of Python. However, because I feel programmers of a 
certain languages bring with them certain vices when moving to other 
languages, I'd like to have feedback from seasoned Python programmers regarding 
my code.</FONT></DIV>
<DIV><FONT face=Arial size=2>Using some socket examples I've googled here and 
there I wrote a very simple Instant-Messaging-wannabe program.</FONT></DIV>
<DIV><FONT face=Arial size=2>Is there some sort of a Wiki where I could post the 
code and have advice on what, how and where to improve? Or should I post the 
code it here?</FONT></DIV>
<DIV><FONT face=Arial size=2>Actually, I'll post it below and if anyone knows of 
a better place to do something like this I'll do it there.</FONT></DIV>
<DIV><FONT face=Arial size=2>Next I'm changing the client to include a server 
thread listening instead of polling the server.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Regards</FONT></DIV>
<DIV><FONT face=Arial size=2>jorge</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>SIMPLE IM CLIENT</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>import socket, threading, time, msvcrt</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>print "Please enter the following 
information"<BR>_url = raw_input("URL: ")<BR>_port = raw_input("Port: 
")<BR>print "Starting IIM client on port: " + _port</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>socketOut = socket.socket(socket.AF_INET, 
socket.SOCK_STREAM)<BR>socketOut.connect((_url, int(_port)))</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2># clear screen here</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>print "Enter your user details"<BR>_from = 
raw_input("User id: ")<BR>_to = raw_input("Buddy id: ")</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>print '\n'<BR>print "Connecting to 
server..."<BR>print '\n'</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2># send user details and receive 
response<BR>socketOut.sendall(<A 
href="mailto:'@@@'+_from+'##'+_to">'@@@'+_from+'##'+_to</A>)<BR>response = 
socketOut.recv(8192)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>def listener():<BR>    while 
1:<BR>        
time.sleep(5)<BR>        
socketOut.sendall('$$$'+_from)<BR>        
response = socketOut.recv(8192)<BR>        if 
response != " 
":<BR>            print 
"\n" + response</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV><FONT face=Arial size=2>
<DIV><BR>if response == 'AUTH_OK':<BR>    data = 
""<BR>    th = 
threading.Thread(target=listener)<BR>    
th.setDaemon(1)<BR>    th.start()<BR>    print 
"Background polling thread started"<BR>    while 1:</DIV>
<DIV> </DIV>
<DIV>        if 
msvcrt.kbhit():<BR>            
ch = msvcrt.getche()<BR>        
else:<BR>            ch = 
None<BR>        if 
ch:<BR>            if ch 
!= 
'\r':<BR>                
data += ch<BR>            
else:<BR>                
print 
'\n'<BR>                
socketOut.sendall('###'+_from+'##'+data)<BR>                
response = 
socketOut.recv(8192)<BR>                
if response != " 
":<BR>                    
print 
response<BR>                
data = ""</DIV>
<DIV> </DIV>
<DIV>else:<BR>    print "Auhentication 
failed!"<BR>        
<BR>socketOut.close()</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>SIMPLE IM SERVER</DIV>
<DIV> </DIV>
<DIV>import SocketServer</DIV>
<DIV> </DIV>
<DIV>_port = 8881<BR>_clients = {}</DIV>
<DIV> </DIV>
<DIV># a connected client<BR>class Client:<BR>    # queue of 
messages sent to this client<BR>    queue = 
[]<BR>    def __init__(self, _sock, _src, 
_dest):<BR>        print "Creating IM 
client"<BR>        self.socket = 
_sock<BR>        print "Incoming socket: %s" 
% self.socket<BR>        self.user = 
_src<BR>        print "Username: " + 
self.user<BR>        # buddies should be a 
list<BR>        self.buddy = 
_dest<BR>        print "Buddy: " + 
self.buddy<BR>        print "Created IM 
client"</DIV>
<DIV> </DIV>
<DIV># the server handling requests<BR>class 
Broker(SocketServer.BaseRequestHandler):<BR>    def 
handle(self):<BR>        print "Connected 
from", self.client_address<BR>        while 
True:<BR>            
receivedData = 
self.request.recv(8192)<BR>            
if not 
receivedData:<BR>                
break<BR>            
<BR>            # if 
handshake packet, extract client 
details<BR>            if 
receivedData.startswith(<A 
href="mailto:'@@@',0,3">'@@@',0,3</A>):<BR>                
print "Received handshake 
packet"<BR>                
# strip handshake 
code<BR>                
receivedData = receivedData.replace(<A href="mailto:'@@@'">'@@@'</A>, '', 
1).lstrip()<BR>                
l = 
receivedData.split('##',1)<BR>                
socket = 
self.request<BR>                
src = 
l[0]<BR>                
dest = 
l[1]<BR>                
c = Client(socket, src, 
dest)<BR>                
# use username as key on 
hashmap<BR>                
_clients[src] = 
c<BR>                
# send success 
message<BR>                
socket.sendall('AUTH_OK')<BR>                
print "Client " + src + " authenticated"</DIV>
<DIV> </DIV>
<DIV>            # if 
polling packet, extract sender details and send 
messages<BR>            
if 
receivedData.startswith('$$$',0,3):<BR>                
# strip polling 
message<BR>                
print "Received polling 
packet"<BR>                
src = receivedData.replace('$$$', '', 
1).lstrip()<BR>                
# only poll if more than 1 
user<BR>                
if len(_clients) > 
1:                
<BR>                    
# use username as key on 
hashmap<BR>                    
_clients[src] = 
c<BR>                    
if len(c.queue) < 
1:<BR>                        
c.socket.sendall(" 
")<BR>                    
else:<BR>                        
msgs = 
""<BR>                        
for q in 
c.queue:<BR>                            
msgs += q + 
'\n'<BR>                            
# send queued 
messages<BR>                        
c.socket.sendall(msgs)<BR>                        
c.queue = 
[]<BR>                        
print "Sent all pending messages for " + 
c.user<BR>                
else:<BR>                    
socket.sendall(" ")</DIV>
<DIV> </DIV>
<DIV>            # if 
message packet, extract data and append to target 
queue<BR>            if 
receivedData.startswith('###',0,3):<BR>                
print "Received message 
packet"<BR>                
receivedData = receivedData.replace('###', '', 
1).lstrip()<BR>                
l = 
receivedData.split('##',1)<BR>                
src = 
l[0]<BR>                
text = 
l[1]<BR>                
if text.strip != 
"":<BR>                    
print "Message not 
empty"<BR>                    
# extract 
client<BR>                    
clientSrc = 
_clients[src]<BR>                    
# ...and its 
buddy<BR>                    
clientDest = 
_clients[clientSrc.buddy]<BR>                    
msg = src+": 
"+text<BR>                    
print "Appended message to queue of " + 
clientSrc.buddy<BR>                    
clientDest.queue.append(msg)<BR>                    
print "Queue of: " + clientDest.user + " = %s" % 
clientDest.queue<BR>                
clientDest.socket.sendall(" 
")<BR>            
else:<BR>                
if len(_clients) < 
2:<BR>                    
self.request.sendall(receivedData)</DIV>
<DIV> </DIV>
<DIV>        for c in 
_clients.values():<BR>            
if self.request == 
c.socket:<BR>                
c.socket.close()<BR>                
# remove from 
hashmap<BR>                
del 
_clients[c.user]<BR>                
print "Removed " + c.user + " from hashmap"</DIV>
<DIV> </DIV>
<DIV>        print "Disconnected from", 
self.client_address<BR>        <BR>srv = 
SocketServer.ThreadingTCPServer(('',_port),Broker)<BR>print "Started IIM server 
on port %d" % _port<BR>srv.serve_forever()</DIV>
<DIV></FONT> </DIV></BODY></HTML>