
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()


with open('test.html') as f:
with open('./test.html') as f:


nikos wrote:Any ideas left?!
template = """\
<html>
...
</html>"""
# rest of script that uses/prints templatetemplate = """\
<html>
...
</html>"""import template1
# rest of script that uses/prints template1.template
#!/usr/bin/python
import cgi
import template
print "Content-type: text/html\n"
template = """\
<html>
<head>
<title> %(title)s </title>
</head>
<body>
<h2> %(message)s </h2>
<h1> %(counter)i </h1>
</body>
</html>
"""
counter = 0
#Now if i could open the template in my host i would sue the following lines
#with open('test.html') as f:
#data = f.read
# Now that i cant how would i read the raw multi-line html above? by using
#data = template ?!?!
for x in range(11):
counter = counter + x
data = data % { 'title': "Nikos!", 'message': "Χαιρετισμούς!", 'counter': counter }
print data

import cgi
print "Content-type: text/html\n"
template = """\
<html>
<head>
<title> %(title)s </title>
</head>
<body>
<h2> %(message)s </h2>
<h1> %(counter)i </h1>
</body>
</html>"""
counter = 0
for x in range(11):
counter = counter + x
print template % {'title': "Nikos!", 'message': "Χαιρετισμούς!", 'counter': counter}
data = """
<html>
<head>
<title> %(title)s </title>
</head>
<body>
<h2> %(message)s </h2>
<h1> %(counter)i </h1>
</body>
</html>
"""
#!/usr/bin/python
import cgi
import template
counter = 0
for x in range(100):
counter = counter + x
data = data % { 'title': "Nikos!", 'message': "Hello, world!!", 'counter': counter }
print (data)

data = template.data % { 'title': "Nikos!", 'message': "Hello, world!!", 'counter': counter }
print (data)import math
print math.factorial(3)import cgitb
cgitb.enable()
#!/usr/bin/python
import cgitb
cgitb.enable()
import cgi
import template
counter = 0
for x in range(20):
counter = counter + x
data = template.data % { 'title': "Nikos!", 'message': "Hello, world!!", 'counter': counter }
print (data)




<html>
<head>
<title> Nikos! </title>
</head>
<body>
<h2> Hello, world!! </h2>
<h1> 190 </h1>
</body>
</html>

import cgitb
cgitb.enable()
import cgi
import template

nikos wrote:Whnat exactly did you say these lines do?
waz wrote:By the way, an easy way to debug CGI scripts is to add these two lines:
- Code: Select all
import cgitb
cgitb.enable()
that way when an error occurs it will print the stack trace on the page as if you had run it from the command line.
print "Location: google.com"

>>> int('3.14')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.14'

Users browsing this forum: No registered users and 1 guest