[Tutor] Python and sockets
Kent Johnson
kent_johnson at skillsoft.com
Tue Aug 17 16:14:18 CEST 2004
When I run your program with no server at port 4001 and click Connect, I
get a traceback in the console:
D:\Personal\Tutor>python sockettest.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python23\lib\lib-tk\Tkinter.py", line 1345, in __call__
return self.func(*args)
File "sockettest.py", line 12, in Connect
Socket.connect(('localhost', 4001));
File "<string>", line 1, in connect
error: (10061, 'Connection refused')
Do you see this when you run it? You can catch the exception in the Connect
function if you want to handle it in another way:
if( Connected == False ):
try:
Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
Socket.connect(('localhost', 4001));
print "Connected to server";
Connected = True;
except socket.error:
print "Connection failure"
By the way your use of globals to pass the Socket and Connected variables
around is a strong hint that you should put Connect(), Disconnect(), Exit()
and Receive() into a class with Socket and Connected as fields.
Also you do not need semicolons for line endings in Python, you just use
semicolons if you want to put two statements on the same line :-)
HTH,
Kent
At 02:43 PM 8/17/2004 +0200, cecilwesterhof at xs4all.nl wrote:
>I started with python and also with sockets. I wrote a little program to
>start using sockets. In principle it works okay. There are only two
>problems:
>
>- When I open a socket and it is refused by the server, I just get a
>socket back, without a signal that the server refused the connection. What
>do I need to do, to see that the connection is refused?
More information about the Tutor
mailing list