I'm creating a python web interface. You complete the form, it sends it to a background python function, stuff happens.
I'm having trouble with one of these background functions, it's stumped me because it's not my first time creating a quick project like this, and I haven't encountered this error before.
I have three files: commands.py, header.py and product.py
product.py looks like this:
#!/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)
and this is the route it takes through header.py:
def printDefaultPage(error,page): #this prints the page
printHTTPHeaders() #print headers
printHeader(page) #print header.inc
printPage(page)
printFooter() #print footer.inc
the above function prints out the headers, the head, the body of the page and the footer, all by calling other functions.
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>"""
as you can see, printPage calls a function, list_subscriptions() which looks a bit like this:
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>"
this in turn calls get_subscriptions, which exists in a file called commands, which is imported at the top:
#!/usr/bin/python
from commands import *
for completeness, here is get_subscriptions:
def get_subscriptions():
subscriptions=proxy.get_subscriptions()
return subscriptions
and on the other end:
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() returns a dictionary. The more baffling thing is when I run printPage("product") in the python command line, it works.
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>
Yet when I go to product.py in the browser expecting to see a nice select box instead I get this gross error:
/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
edit: the files are all chmod to 755, and are all owned by www-data.
edit: in the python command line printDefaultPage(0,"product") returns this:
>>> 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"> </p>
</div>
</div>
</br>
<center><div id="footerlog">Encryptor v3 <span id="dot"$bull;</span>Copyright © 2015 <a href="https://www.encription.co.uk" target="_ne$
</div></center>
</body>
</html>
which is as expected, however is not working in the browser.
I've found the issue! Giving up on this I started working on another page, however soon the same problems began to surface.
commands.py is already used by python, meaning I was importing the wrong module all along. Oopsie!