简体   繁体   English

为什么“ url”在这里不能作为变量使用?

[英]Why does 'url' not work as a variable here?

I originally had the variable cpanel named url and the code would not return anything. 我最初有一个名为url的变量cpanel,该代码将不返回任何内容。 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']

Note that urllib2.Request has a parameter named url , but that really shouldn't be the source of the problem, it works as expected: 请注意, urllib2.Request有一个名为url的参数,但这实际上不应该是问题的根源,它可以按预期工作:

>>> 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 . 请注意,当您将cpanel切换为url时,上述代码的功能相同。 So the problem must have been elsewhere. 因此问题肯定出在其他地方。

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. 我很确定/cpanel (如果它是托管控制面板)实际上将您重定向(302)到http://www.tas-tech.com:2082/或类似的东西。 You should just update your thing to deal with the redirect (or better yet, just send the request to the real address). 您应该只更新您的内容以处理重定向(或者更好的是,将请求发送到真实地址)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM