[Tutor] Socket Programming
Steven D'Aprano
steve at pearwood.info
Mon Apr 8 14:26:28 CEST 2013
On 08/04/13 17:06, Mousumi Basu wrote:
> I want to perform binding between two computers having ip addresses
> 172.18.2.11 and 172.18.2.95.So i wrote the following code(on the computer
> having IP address 172.18.2.95):-
>
>
> import socket
> import sys
> s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
> try:
> s=s.bind(('172.18.2.11',2213))
> print 'socket bind is complete'
> except socket.error,msg:
> print 'bind failed'
> sys.exit()
Why are you throwing away the useful information Python gives you to debug the
problem? That's like trying to find your lost keys by deliberately putting on a
blindfold.
Never, never, never catch an exception only to print a useless message that
tells you nothing. "Bind failed"? Why did it fail? Who knows! You threw that
information away. You might as well have said "An error occurred".
Instead, use this code:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s = s.bind(('172.18.2.11', 2213))
print 'socket bind is complete'
If an error occurs, Python will automatically print a full traceback showing
you exactly:
* what went wrong
* where it went wrong
* what line of code caused the problem
* what type of problem it was
instead of just a useless "an error occurred" message.
Once you have a proper traceback, and you can see the actual error message,
then and only then can we begin to solve the problem.
--
Steven
More information about the Tutor
mailing list