tags:

views:

77

answers:

2

I originally had the variable cpanel named url and the code would not return anything. Any idea why? It doesn't seem to be used by anything else, but there's gotta be something I'm overlooking.

import urllib2

cpanel = 'http://www.tas-tech.com/cpanel'
req = urllib2.Request(cpanel)
try:
    handle = urllib2.urlopen(req)
except IOError, e:
    if hasattr(e, 'code'):
        if e.code != 401:
            print 'We got another error'
            print e.code
        else:
            print e.headers
            print e.headers['www-authenticate']
A: 

I'm pretty sure that /cpanel (if it is the hosting control panel) actually redirects (302) you to http://www.tas-tech.com:2082/ or something like that. You should just update your thing to deal with the redirect (or better yet, just send the request to the real address).

webdestroya
Ah, thanks. I did later notice I got the 302 'error' when before I wasn't while using the new variable. Not sure what changed, but either way I can't reproduce it now so all is well.
kryptobs2000
+2  A: 

Note that urllib2.Request has a parameter named url, but that really shouldn't be the source of the problem, it works as expected:

>>> import urllib2
>>> url = "http://www.google.com"
>>> req = urllib2.Request(url)
>>> urllib2.urlopen(req).code
200

Note that your code above functions identically when you switch cpanel for url. So the problem must have been elsewhere.

ChristopheD
Ah, yup, thanks man. Pretty sure webdestroya found out what the problem was; something to do with a redirect; not sure why it's not happening now, but that makes enough sense to me.
kryptobs2000