[Pythonmac-SIG] client + server

Bob Ippolito bob at redivi.com
Mon Jun 12 19:17:23 CEST 2006


On Jun 12, 2006, at 9:15 AM, Feat wrote:

> I need some help to make this simple program run [please, see the  
> source code below] as it's probably some basic misunderstanding.  
> I'm trying to lay out the principles of an elementary chat server  
> with a fixed number of clients, and I've been testing these two  
> programs on my local network -- WiFi, no fire wall.
>
> The client would be an iMac G5 running X.4.6 at 192.168.1.20 while  
> the server would be a PowerBook running X.3.9 at 192.168.1.129.  
> That said,  swapping the machines doesn't solve anything.
>
> The question is: why can't the client use the socket.connect  
> function here?

The first thing is that it's really hard to read because the naming  
conventions and spacing are very strange. You should give PEP 8 a  
good read and get used to writing Python code the way most other  
people do:
http://www.python.org/dev/peps/pep-0008/

The reason that it doesn't work is that your connect function is  
totally busted:

> def connect (Host, Port, P):
> 	Sk = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
> 	try:
> 		Sk.socket.connect ((Host, Port))
> 	except :
> 		print "Connection failed..."
> 		sys.exit ()
> 	print "Connection successful @ ", Host , Port
> 	Sk.socket.send (P)
> 	return Sk

Sockets don't have a socket attribute. It should be just "sk.connect 
((host, port))" and "sk.send(p)". This is also why you should never  
use a bare try:except: statement. It should really only be catching  
socket.error; logging the exception is usually a good idea as well.  
In this case you're getting an AttributeError, which is clearly not  
what you'd expect.

Another reason that it might not work (once you fix connect) is that  
the server listens on ('localhost', 5007) by default... you probably  
want to listen on ('', 5007) or ('0.0.0.0', 5007) if you want other  
machines to connect to you.

-bob


More information about the Pythonmac-SIG mailing list