I'm trying to find out why I'm seeing this. This is the code-snippet:
def check_status(addr,port=80):
import urllib2
if not addr.startswith('http://'): addr = "http://" + addr
req = urllib2.Request(addr+":"+str(port))
try:
res = urllib2.urlopen(req,None,10)
sts = str(res.code)
except urllib2.URLError, er:
sts = er.args
print "Print-1\t: %s" % sts
print "Print-2\t:", sts
print "{0}\t: {1}".format('Print-3',sts)
return sts
url = "google.comm"
sts = check_status(url)
print "Print-4\t: %s %s" % (url, sts)
Running the script, I get interesting result on print statement:
Print-1 : [Errno 8] nodename nor servname provided, or not known
Print-2 : (gaierror(8, 'nodename nor servname provided, or not known'),)
Print-3 : (gaierror(8, 'nodename nor servname provided, or not known'),)
Print-4 : google.comm (gaierror(8, 'nodename nor servname provided, or not known'),)
Can anyone please explain why printing sts
coming differently for print-2, 3 and 4? It's printing in correct format only with single %s
formatting-string. I don't think it's got anything to do with urllib2. What am I missing here? Thanks!
sts = str(res.code)
in the success case, but notsts = str(er.args)
in the error case? (If you'd done that, this issue would never have come up, and you wouldn't have learned something new, so it's probably for the best. It's just that I'm having a hard time understanding why an integer isn't an acceptable value to pass around, but a tuple of arbitrary exception arguments is.) – abarnert May 30 '13 at 19:12str(er.args)
doesn't change any thing for me at all. Cheers!! – MacUsers May 30 '13 at 19:34