| | |
python cgi?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hi, i currently have these 2 files:
and
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?
Python Syntax (Toggle Plain Text)
#!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
Python Syntax (Toggle Plain Text)
#!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?
don't judge me because I'm a year 8!
'it is better to fight for something than to live for nothing'General George S Patton
'it is better to fight for something than to live for nothing'General George S Patton
•
•
Join Date: Nov 2009
Posts: 79
Reputation:
Solved Threads: 21
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'], 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[i] such that a <= x[i] < b. That is, range(1, 4) gives you [1, 2, 3]. You probably want range(1, b+1).
b = a['b'] 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.
After obtaining b = a['b'], 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[i] such that a <= x[i] < b. That is, range(1, 4) gives you [1, 2, 3]. You probably want range(1, b+1).
b = a['b'] 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 Syntax (Toggle Plain Text)
#!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 Syntax (Toggle Plain Text)
#!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>'
"I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum."
------------
This posts may be redistributed under the Creative Commons BY-SA License.
------------
This posts may be redistributed under the Creative Commons BY-SA License.
•
•
Join Date: Nov 2009
Posts: 79
Reputation:
Solved Threads: 21
•
•
•
•
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?
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.
Last edited by pythopian; 25 Days Ago at 6:50 am.
"I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum."
------------
This posts may be redistributed under the Creative Commons BY-SA License.
------------
This posts may be redistributed under the Creative Commons BY-SA License.
•
•
Join Date: Nov 2009
Posts: 79
Reputation:
Solved Threads: 21
•
•
•
•
okay thanks, so you're saying set up a dedicated server when I go public with this?
Last edited by pythopian; 25 Days Ago at 1:16 pm.
•
•
Join Date: Nov 2009
Posts: 79
Reputation:
Solved Threads: 21
0
#9 25 Days Ago
•
•
•
•
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?
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.
Last edited by pythopian; 25 Days Ago at 2:06 pm.
"I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum."
------------
This posts may be redistributed under the Creative Commons BY-SA License.
------------
This posts may be redistributed under the Creative Commons BY-SA License.
![]() |
Similar Threads
- python cgi (Python)
- Python CGI error (Python)
- mod_python apache2.2 Trouble in running python cgi scrpts (Python)
- html & python cgi script help... (Python)
- Python, Cgi and MySQL (Python)
- number of rows in an html file using python cgi (Python)
- Dreamweaver + Python + CGI Script (Python)
- A subtle python cgi problem (Python)
- Python CGI and html enctype conflict? (Python)
Other Threads in the Python Forum
- Previous Thread: Connect 4 Print Trouble
- Next Thread: Iterating over large number of files
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format ftp function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlwt





