Python-list Digest, Vol 96, Issue 137

Ricardo Mansilla rick.mansilla at gmail.com
Sat Sep 24 20:50:26 CEST 2011


On Saturday 24 September 2011 01:48:29 you wrote:
> Ricardo wrote:
> > Hi everyone
> > I'm trying to use the cgi library to create a python script and loading
> > it from a web page. I have already done the necessary imports, and the
> > default commands to receive data from "html" are written too. The final
> > version is something like this:
> > 
> > #!/usr/bin/python
> > 
> > import subprocess
> > import cgi
> > import cgitb
> > 
> > cgitb.enable()
> > 
> > input = cgi.FieldStorage()
> > 
> > …. my code (do something with input)….
> > 
> > 
> > #printing the response
> > 
> > print "Content-Type: text/html"
> > print
> > print "<TITLE>My title:</TITLE>"
> > print "</HEAD>"
> > print "<BODY>"
> > print ….. bla bla …
> > print "%s"%theoutput
> > print "</BODY>"
> > 
> > Besides, my call from my index.html is like this:
> >  <form action="/scripts/python_script.py" method="post">
> >  
> >       <input name="inid" type="text" size="20" class="input" /><br/><br/>
> >       
> >         <input type="submit" value="accept"  class="button"/>
> >  
> >  </form>
> > 
> > well, the thing is that when i do the call from the browser:
> > 
> > http://localhost/index.html
> > 
> >           V
> > 
> > put the data and click on the "accept" button
> > 
> >           V
> > 
> > http:/localhost/scripts/python_script.py
> > 
> > I only get the python_script.py as a plain test by response (the script
> > printed on my browser). I have already changed the permissions for
> > python_script.py. I have checked the import cgi,cgitb in the python shell
> > (i am using v2.7) and they work fine. So, i don't know what it is going
> > wrong here.
> > 
> > A little help please… any idea?
> 
> Is your webserver configured to allow cgi scripts? In the scripts
> directory? For Apache see
> 
> http://httpd.apache.org/docs/current/howto/cgi.html
> 
> Python also comes with a CGI Server. A quick-and-dirty setup goes like
> this:
> 
> $ cat cgi-bin/script.py
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
> 
> import cgi
> import cgitb
> 
> cgitb.enable()
> 
> input = cgi.FieldStorage()
> 
> print "Content-Type: text/html"
> print
> print "<TITLE>My title:</TITLE>"
> print "</HEAD>"
> print "<BODY>"
> print "Hello world"
> print "</BODY>"
> $ chmod a+x cgi-bin/script.py
> $ python -m CGIHTTPServer
> Serving HTTP on 0.0.0.0 port 8000 ...
> 
> If you then point your browser to http://localhost:8000/cgi-bin/script.py
> you should see
> 
> Hello world
> 
> in the browser and (something like)
> 
> localhost - - [24/Sep/2011 08:41:27] "GET /cgi-bin/script.py HTTP/1.1" 200
> -
> 
> in the shell. Note that the script must be in cgi-bin (or htbin) unless you
> start the server with a custom script that modifies
> CGIHTTPRequestHandler.cgi_directories accordingly.


Thanks a lot, for your answer. Yes, i can run scripts from /cgi-bin/. Actually 
I follow you example and it works really well. I didn't know at all about this 
CGI server. 
I am doing the hole thing over python now, it's nice. 
Thanks again.

-- 
(...)Also, since that same law states that any system able to prove its 
consistency to itself must be inconsistent; any mind that believes it can 
prove its own sanity is, therefore, insane.(...) 
Kurt Gödel. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110924/af016d3e/attachment.html>


More information about the Python-list mailing list