python cgi?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 399
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz

python cgi?

 
0
  #1
32 Days Ago
Hi, i currently have these 2 files:

  1. #!C:\Python26\python.exe
  2.  
  3. #index.py
  4. print 'Content-type: text/html\n\n'
  5. print '<html><head>'
  6. print '<title>My Page</title>'
  7. print '</head><body>'
  8. print '<h1>Powers of two</h1>\n<ol>'
  9. print '<form action="sqr.py" method="post">'
  10. print '<label>How many numbers?</label>'
  11. print '<input type="text" name="b"/>'
  12. print '</ol></body></html>'

and

  1. #!C:\Python26\python.exe
  2. #sqr.py
  3. import cgi
  4. a = cgi.FieldStorage()
  5. b = a["b"]
  6. print 'Content-type: text/html\n\n'
  7. print '<html><head>'
  8. print '<title>My Page</title>'
  9. print '</head><body>'
  10. print '<h1>Powers of two</h1>\n<ol>'
  11. for n in range(1,b):
  12. print '<li>'+str(2**n)+'</li>'
  13. 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 108
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 12
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster
 
0
  #2
31 Days Ago
Not quite sure what to mean...
NOTE: sudo doesn't apply to real life situations.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 79
Reputation: pythopian is an unknown quantity at this point 
Solved Threads: 21
pythopian pythopian is offline Offline
Junior Poster in Training

Several things

 
0
  #3
26 Days Ago
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.

  1. #!python
  2.  
  3. #index.py
  4. import cgitb; cgitb.enable()
  5. print '''Content-type: text/html\n
  6. <html><head>
  7. <title>My Page</title>
  8. </head><body>
  9. <h1>Powers of two</h1>\n<ol>
  10. <form action="sqr.py" method="post">
  11. <label>How many numbers?</label>
  12. <input type="text" name="b" />
  13. <input type="submit" name="Submit" />
  14. </ol></body></html>'''

  1. #!python
  2. #sqr.py
  3. import cgitb; cgitb.enable()
  4. import cgi
  5. a = cgi.FieldStorage()
  6. b = int( a.getvalue('b', '0') );
  7. print '''\
  8. Content-type: text/html\n
  9. <html><head>
  10. <title>My Page</title>
  11. </head><body>
  12. <h1>Powers of two</h1>\n<ol>'''
  13. for n in range(1,b+1):
  14. print '<li>'+str(2**n)+'</li>'
  15. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 399
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz
 
0
  #4
25 Days Ago
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?
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 79
Reputation: pythopian is an unknown quantity at this point 
Solved Threads: 21
pythopian pythopian is offline Offline
Junior Poster in Training

Apache works for me

 
0
  #5
25 Days Ago
Originally Posted by leegeorg07 View Post
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 399
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz
 
0
  #6
25 Days Ago
okay thanks, so you're saying set up a dedicated server when I go public with 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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 79
Reputation: pythopian is an unknown quantity at this point 
Solved Threads: 21
pythopian pythopian is offline Offline
Junior Poster in Training

OMG, No! I'm not saying that.

 
0
  #7
25 Days Ago
Originally Posted by leegeorg07 View Post
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.
Last edited by pythopian; 25 Days Ago at 1:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 399
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz
 
0
  #8
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?
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 79
Reputation: pythopian is an unknown quantity at this point 
Solved Threads: 21
pythopian pythopian is offline Offline
Junior Poster in Training
 
0
  #9
25 Days Ago
Originally Posted by leegeorg07 View Post
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 399
Reputation: leegeorg07 is an unknown quantity at this point 
Solved Threads: 31
leegeorg07's Avatar
leegeorg07 leegeorg07 is offline Offline
Posting Whiz
 
0
  #10
25 Days Ago
oh thanks that's great!
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC