<type'exception.nameerror'=“”>:全球名称&#39; ..&#39;未定义</type>

时间:2015-02-26 11:33:58

标签: python html web cgi xmlrpclib

我正在创建一个python web界面。你完成表单,它将它发送到后台python函数,发生了一些事情。

我在使用其中一个后台功能方面遇到了麻烦,它让我很难过,因为这不是我第一次创建像这样的快速项目,而我还没有遇到过这个问题。错误之前。

我有三个文件:commands.py,header.py和product.py

product.py看起来像这样:

#!/usr/bin/python
import sys
import os
import cgitb
cgitb.enable()

sys.path.append(os.path.abspath("/var/www/cgi-bin/includes/"))
from header import *

page = get_page_name(sys.argv[0])

printDefaultPage(0,page)

这是它通过header.py:

的路由
def printDefaultPage(error,page): #this prints the page
        printHTTPHeaders() #print headers
        printHeader(page) #print header.inc
        printPage(page)
        printFooter() #print footer.inc

上述功能通过调用其他功能打印出页眉,页眉,页面正文和页脚。

def printPage(page):
        if page == "product":
                print """<form method='post' id="register_form" name="client_frm"></br>
                <label for="id">Enter the subscription to use:</label>"""
                list_subscriptions()
                print """<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
                </form>"""

正如你所看到的,printPage调用了一个函数list_subscriptions(),看起来有点像这样:

def list_subscriptions():
        subscriptions=get_subscriptions()
        print """<select name = "sub">"""
        for s in subscriptions:
                id = s
                price = subscriptions.get(str(s)).get('price')
                scans = subscriptions.get(str(s)).get('scans_per_month')
                print """<option value ="""+str(id)+"""">"""+str(price)+""" - """+str(scans)+"""</option>"""
        print "</select>"

这反过来调用get_subscriptions,它存在于一个名为commands的文件中,该文件在顶部导入:

#!/usr/bin/python
from commands import *

为了完整性,这里是get_subscriptions:

def get_subscriptions():
        subscriptions=proxy.get_subscriptions()
        return subscriptions

在另一端:

def get_subscriptions(self):
        subscriptions=self.store.find(Subscription)
        subs = {}
        for sub in subscriptions:
                subs[str(sub.id)] = {}
                subs[str(sub.id)]['scans_per_month'] = sub.scans_per_month
                subs[str(sub.id)]['price'] = int(sub.price)
        return subs

get_subscriptions()返回一个字典。 更令人困惑的是当我在python命令行中运行printPage(&#34; product&#34;)时,它可以工作。

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from header import *
>>> printPage("product")
<form method='post' id="register_form" name="client_frm"></br>
                <label for="id">Enter the subscription to use:</label>
<select name = "sub">
<option value =1">19 - 20</option>
<option value =3">59 - 100</option>
<option value =2">39 - 50</option>
<option value =5">0 - 0</option>
<option value =4">0 - 1000</option>
</select>
<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
            </form>

然而,当我在浏览器中访问product.py期望看到一个不错的选择框时,我得到了这个严重错误:

 /var/www/cgi-bin/includes/header.py in list_subscriptions()
      7 
      8 def list_subscriptions():
=>    9         subscriptions=get_subscriptions()
     10         print """<select name = "sub">"""
     11         for s in subscriptions:
subscriptions undefined, get_subscriptions undefined

编辑:这些文件都是chmod到755,并且全部归www-data所有。

编辑:在python命令行中printDefaultPage(0,&#34; product&#34;)返回:

>>> printDefaultPage(0,"product")
Content-Type: text/html
Vary: *
Title: Encriptor
Cache-Control: no-cache, no store, must-revalidate

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <title>Encriptor Scanner</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="copyright" content="encription.co.uk" />
        <meta name="language" content="en" />
        <link rel="shortcut icon" href="/css/images/favicon.ico" />
        <link href="/css/style.css" rel="stylesheet" type="text/css" />
<body>
 <div class="wrap" 
><div id="header">
        <p id="logname"></p>
    <img src="/images/raptor-logo.png" border="0" 
id="logo-main"
/></div>
<ul class="menu">
</ul>
<div id="content">
<form method='post' id="register_form" name="client_frm"></br>
            <label for="id">Enter the subscription to use:</label>
<select name = "sub">
<option value =1">19 - 20</option>
<option value =3">59 - 100</option>
<option value =2">39 - 50</option>
<option value =5">0 - 0</option>
<option value =4">0 - 1000</option>
</select>
<a href="/cgi-bin/index.py"><input class="login" type="button" value="Back"></a>
        </form>
</div>
        <p id="spacer">&nbsp;</p>
        </div>
        </div>  
        </br>
        <center><div id="footerlog">Encryptor v3 <span id="dot"$bull;</span>Copyright &copy; 2015 <a href="https://www.encription.co.uk" target="_ne$
        </div></center>
        </body>
        </html>

这是预期的,但是在浏览器中不起作用。

1 个答案:

答案 0 :(得分:0)

我发现了这个问题! 放弃这一点,我开始在另一页上工作,但很快就会出现同样的问题。

python已经使用了

commands.py,这意味着我一直在导入错误的模块。 Oopsie!