I am using python 3.5 IDLE, windows 10 and edge as browser. I am facing issue while executing following simple code on simple HTTP server. Issue is that it just displays the code after removing the HTML tab instead of showing “ Hello World! This is my first CGI program “.
Have I missed anything in setting of environmental variables etc?
Code:
import cgi, cgitb
print ("Content-type:text/html\n\n")
print ('<html>')
print ('<head>')
print ('<title>Hello Word - First CGI Program</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello World! This is my first CGI program</h2>')
print ('</body>')
print ('</html>')
I have been able to get the code you have written working without issue, so perhaps it is an issue in your calling environment.
If you see this section of the cgi docs: https://docs.python.org/3/library/cgi.html then
If it’s installed in the standard cgi-bin directory, it should be possible to send it a request by entering a URL into your browser of the form:
http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home
as well as the cgi section of the python HTTP Server docs here: https://docs.python.org/3/library/http.server.html#http.server.CGIHTTPRequestHandler
cgi_directories
This defaults to ['/cgi-bin', '/htbin']
and describes directories to treat as containing CGI scripts.
What we need then is a certain folder structure, as in:
cgi-test
|
+-- cgi-bin
|
+-- test.py
with test.py
containing your code as above.
Now to start the python HTTP Server navigate to your enclosing folder (ie cgi-test
) and type in python -m http.server --cgi 8000
and assuming your python executable is on your system path you should get a response like: Serving HTTP on 0.0.0.0 port 8000 ...
Finally navigate using your browser to http://localhost:8000/cgi-bin/test.py
to see your rendered page.
Note: cgi-test
could be anything, for instance C:\Users\Username\Website, D:\MyServer\ ...
Note2: If I move the test.py
file up one level to the cgi-test folder I get a result similar to what you describe (ie I see the code rather than the rendered page)
Note3: I am running on Windows 7 with python 3.4, but it should hopefully be similar enough that you should be able to follow the same procedure.