whats this mean?
Bill Bell
bill-bell at bill-bell.hamilton.on.ca
Sat Jul 21 08:53:40 EDT 2001
thedustbustr at aol.com (TheDustbustr) wrote, in part:
> # BEGIN CODE BLOCK
> try:
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind((HOST, PORT))
> s.listen(1)
> conn, addr = s.accept()
> except socket.error, why: ## reference 1
> print "Server Error: " + errno.errorcode[why[0]] + ", " + why[1]
> ##
> reference 2
> sys.exit(1)
> # END CODE BLOCK
>
> what does reference 2 mean? what do those brackets do? And for
> reference 1, what will 'why' hold if an error occurs?
Dustin,
Consider the following code that is similar to what you included in
your message.
from errno import *
import socket
try:
raise socket.error ( 1, "an error message" )
except socket.error, why:
print why
print why [ 0 ]
print why [ 1 ]
print "Error: " + errno.errorcode[why[0]] + ", " + why[1]
I have replaced the sockets code with a 'raise' statement that does
what the sockets code would do in case of an exception.
When I executed this code I got the following output:
(1, 'an error')
1
an error
Error: EPERM, an error
ie, one line for each 'print' statement in the exception suite.
1. 'why' will contain a tuple whose elements are the arguments
passed to the procedure socket.error
2. 'why[0]' is the first element in the tuple and thus is the first
argument to socket.error; likewise 'why[1]' will contain the second
argument.
3. 'errno.errorcode' supplies the os-specific code (ie the code that
is meaningful for the os) corresponding to the value of the first
argument to 'socket.error'
A pair of brackets indexes into the expression to the left of the pair.
For example, '[0]' in 'why[0]' returns the 0th item in 'why'.
How well does this clarify the code?
Bill
More information about the Python-list
mailing list