944,092 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 861
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 4th, 2009
0

python cgi?

Expand Post »
Hi, i currently have these 2 files:

Python Syntax (Toggle Plain Text)
  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

Python Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 35
Solved Threads: 32
Posting Pro in Training
leegeorg07 is offline Offline
428 posts
since Jul 2008
Nov 5th, 2009
0
Re: python cgi?
Not quite sure what to mean...
Reputation Points: 35
Solved Threads: 22
Junior Poster
ov3rcl0ck is offline Offline
113 posts
since Sep 2009
Nov 10th, 2009
0

Several things

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.

Python Syntax (Toggle Plain Text)
  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>'''

Python Syntax (Toggle Plain Text)
  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>'
Reputation Points: 20
Solved Threads: 25
Junior Poster in Training
pythopian is offline Offline
81 posts
since Nov 2009
Nov 11th, 2009
0
Re: python cgi?
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?
Reputation Points: 35
Solved Threads: 32
Posting Pro in Training
leegeorg07 is offline Offline
428 posts
since Jul 2008
Nov 11th, 2009
0

Apache works for me

Click to Expand / Collapse  Quote originally posted by leegeorg07 ...
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; Nov 11th, 2009 at 6:50 am.
Reputation Points: 20
Solved Threads: 25
Junior Poster in Training
pythopian is offline Offline
81 posts
since Nov 2009
Nov 11th, 2009
0
Re: python cgi?
okay thanks, so you're saying set up a dedicated server when I go public with this?
Reputation Points: 35
Solved Threads: 32
Posting Pro in Training
leegeorg07 is offline Offline
428 posts
since Jul 2008
Nov 11th, 2009
0

OMG, No! I'm not saying that.

Click to Expand / Collapse  Quote originally posted by leegeorg07 ...
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; Nov 11th, 2009 at 1:16 pm.
Reputation Points: 20
Solved Threads: 25
Junior Poster in Training
pythopian is offline Offline
81 posts
since Nov 2009
Nov 11th, 2009
0
Re: python cgi?
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?
Reputation Points: 35
Solved Threads: 32
Posting Pro in Training
leegeorg07 is offline Offline
428 posts
since Jul 2008
Nov 11th, 2009
0
Re: python cgi?
Click to Expand / Collapse  Quote originally posted by leegeorg07 ...
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; Nov 11th, 2009 at 2:06 pm.
Reputation Points: 20
Solved Threads: 25
Junior Poster in Training
pythopian is offline Offline
81 posts
since Nov 2009
Nov 11th, 2009
0
Re: python cgi?
oh thanks that's great!
Reputation Points: 35
Solved Threads: 32
Posting Pro in Training
leegeorg07 is offline Offline
428 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Connect 4 Print Trouble
Next Thread in Python Forum Timeline: Iterating over large number of files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC