[Tutor] exception problems in socket programming

Kent Johnson kent37 at tds.net
Thu Nov 16 12:05:34 CET 2006


Vinay Reddy wrote:
> Hi,
> I'm trying to do some simple network programming in Python, but am
> stuck with exception problems. Here's the relevant code snippet:
> 
> self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>       try:
>          self.s.connect((something.com, 5000))
>       except socket.error, (value, message):

This is equivalent to
   except socket.error, e:
   (value, message) = e

in other words you are tuple-unpacking the exception. Since it is not a 
tuple, but a single value, you get an error. Try this:
       except socket.error, e:
          if self.s:
             self.s.close()
          print "Could not open socket: " + e.message

Kent


>          if self.s:
>             self.s.close()
>          print "Could not open socket: " + message
>          sys.exit(1)
> 
> I was testing for a timeout exception (which is 60 seconds by
> default), but after the timeout I get the following error:
>   File "./client.py", line 54, in connect
>     except socket.error, (value, message):
> ValueError: need more than 1 value to unpack
> 
> I googled and found something on ValueError, but I couldn't see how it
> applied here.
> 
> I'd appreciate any help.
> 
> Thanks,
> Vinay
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list