Hi, i currently have these 2 files:

#!C:\Python26\python.exe

#index.py
print 'Content-type: text/html\n\n'
print '<html><head>'
print '<title>My Page</title>'
print '</head><body>'
print '<h1>Powers of two</h1>\n<ol>'
print '<form action="sqr.py" method="post">'
print '<label>How many numbers?</label>'
print '<input type="text" name="b"/>'
print '</ol></body></html>'

and

#!C:\Python26\python.exe
#sqr.py
import cgi
a = cgi.FieldStorage()
b = a["b"]
print 'Content-type: text/html\n\n'
print '<html><head>'
print '<title>My Page</title>'
print '</head><body>'
print '<h1>Powers of two</h1>\n<ol>'
for n in range(1,b):
    print '<li>'+str(2**n)+'</li>'
print '</ol></body></html>'

and it produces reasonable responses, except for when i press enter the page only shows the powers of two part, what can I do to fix this?

I wonder you haven't got exceptions. Use import cgitb; cgitb.enable() at the beginning of your scripts to display them.

After obtaining b = a, b is a string, and range(1, b) will crash. You need to convert it to int first.

range(1, b) is probably not quite what you want. range(a, b) gives you a list of elemens x such that a <= x < b. That is, range(1, 4) gives you [1, 2, 3]. You probably want range(1, b+1).

b = a is probably not a good idea anyway, as it will crash when you don't supply b. Rather use b = a.getfirst('b', '0')

Some servers don't support POST requests (SimpleHTTPD, for ex.) This may lead to b being missing in sqr.py. You should use GET during development, especially at the beginning. It also makes it easier to test your CGI scripts, as you can pass all parameters in the URL. You can switch to POST later, for production.

Instead of the many print's, use a multiline string - it will make writing long chunks of HTML a lot easier.

The she-bang line should ideally read #!python to be portable between your local computer and the actual server. (It is not very likely C:\Python26 will be the path to Python everywhere.) When python.exe is on the PATH, #!python works just fine.


#!python

#index.py
import cgitb; cgitb.enable()
print '''Content-type: text/html\n
<html><head>
<title>My Page</title>
</head><body>
<h1>Powers of two</h1>\n<ol>
<form action="sqr.py" method="post">
<label>How many numbers?</label>
<input type="text" name="b" />
<input type="submit" name="Submit" />
</ol></body></html>'''
#!python
#sqr.py
import cgitb; cgitb.enable()
import cgi
a = cgi.FieldStorage()
b = int( a.getvalue('b', '0') );
print '''\
Content-type: text/html\n
<html><head>
<title>My Page</title>
</head><body>
<h1>Powers of two</h1>\n<ol>'''
for n in range(1,b+1):
    print '<li>'+str(2**n)+'</li>'
print '</ol></body></html>'

oh okay that's great thanks, i was using the long link because I'm using it on my Apache server to learn it, can you recommend any good free servers that support python?

oh okay that's great thanks, i was using the long link because I'm using it on my Apache server to learn it, can you recommend any good free servers that support python?

Actually, the web server is independent of the programming language you use for your CGI script. The HTTP protocol defines GET and POST requests (among others), but the web server needs to support them, in order to pass on the request parameters to the script. If it doesn't, the script won't see any parameters.

Apache works very well for me in this respect (and in any other, too). Many python web frameworks provide a standalone web server, but (a) it is usually not as reliable as Apache for production sites, and (b) you can't usually use them with inexpensive shared server hosting plans, because they don't allow a user program to run permanently. I'd recommend to stick to Apache.

FYI, examples of reliable, mature python web servers are Zope and Twisted. But they impose extra requirements on the server configuration / hosting model. The vast majority of web hosts don't support them.

Edited 7 Years Ago by pythopian: n/a

okay thanks, so you're saying set up a dedicated server when I go public with this?

okay thanks, so you're saying set up a dedicated server when I go public with this?

On the contrary. Twisted and Zope have a slow "learning curve", and dedicated servers are much too expensive for a bit of CGI. What I said was that Apache should work just fine. (The code I provided before runs w/o problems on my local Apache server.) My advice is: Use Apache.

Edited 7 Years Ago by pythopian: n/a

yeah I understand that but what I mean is should i set one up if I use python with paid websites and host the site on there?

yeah I understand that but what I mean is should i set one up if I use python with paid websites and host the site on there?

I'm not quite sure I understand your question.

If you just need to play around with python web programming for yourself, do it on a local Apache server. (You would develop on a local server anyway.)

If you want a public website, you have hardly another choice but to host it on a remote (payed) server. But you don't have to rush into a dedicated hosting plan. If you only need a blog (say WordPress), plus a place to try out some python CGI, the least expensive hosting plan will do. You need to make sure that the web host supports python in the first place. Many don't, and if they do, they usually only support CGI, but not mod_wsgi or mod_python.

If your site has to be public, AND you want to implement all/most of it in python, then you'll have to go for web hosting with thorough python support. Here is an article with more information on python hosting.

Edited 7 Years Ago by pythopian: n/a

This question has already been answered. Start a new discussion instead.