Ray
Ray

Reputation: 787

Running python scripts from lighttpd - Firefox asking to save file

I am trying to run a simple Python script from my lighttpd server. The HTML code is:

<html>
<title>Interactive page</title>
<body>
    <form method=POST action="cgi-bin/cgi101.py">
        <P><B>Entery your name: </B>
        <P><input type=text name=user>
        <P><input type=submit>
    </form>
</body>
</html>

My Python script is:

#!/usr/bin/python
import cgi
form = cgi.FieldStorage()
print('Content type:text/html \n')
print('<title>Reply Page</title>')
if not 'user' in form:
    print('<h1>who are you</h1>')
else:
    print(cgi.escape(form['user'].value))

So my question is, am I able to load the HTML page and then I click on Submit Query? Firefox is asking "You have chosen to open cgi101.py" from localhost and asking what should Firefox do with this file and whether I want to save it. Shouldn't it just open in Firefox and run the Python script rather than asking me to save the Python script?

Upvotes: 0

Views: 985

Answers (2)

roy
roy

Reputation: 26

I just have the same problem yesterday when I'm learning the Programing Python book.I find the answer after that is you loss an important step.That is you have lost a webserver.py file which implements an HTTP web server in Python that knows how to run server-side CGI scripts coded in Python; serves files and scripts from current working dir.Here is the code:

`import os,sys
from http.server import HTTPServer,CGIHTTPRequestHandler

webdir = '.'
port = 80

os.chdir(webdir)
srvraddr = ("",port)
srvrobj = HTTPServer(srvraddr,CGIHTTPRequestHandler)
srvrobj.serve_forever()`

you can try it again!

Upvotes: 1

Ray
Ray

Reputation: 787

Found the solution add chmod +x file name to make it executable.

Upvotes: 0

Related Questions