3

So I am trying to keep my project as simple as possible, therefore I have decided to use a CGI with my Python scripts in order to run a program that does something.

So here is my current setup:

In CMD, I run: python -m http.server --cgi 8000

This start a server for me. I can access it via localhost:8000.

Next, I am trying to find my directory with the script by typing in the actual address where it is located: localhost:8000/test/cgi-bin/test.py

This is giving me the output of the actual file, not actually reading it properly. I have tried 2 different ways to output data on the Python file, for example:

import sys
sys.stdout.write("Content-type: text\html \r\n\r\n")
sys.stdout.write("<html><title>Hi</title><body><p>This is a test</p></body></html>")

and

print("Content-Type: text/html\n")
print("<!doctype html><title>Hello</title><h2>hello world</h2>")

Both of which result in the actual code being displayed in my browser.

A few questions:

  • How do I get my server to automatically take me to the location of the file I am trying to run?
  • How do I get the python script to output the proper stuff?
  • Am I setting this up correctly?

I am trying to avoid installing any new dependencies and keep it as minimal as possible.

I am running on Python3, Windows7. I am trying to avoid downloading more pip packages and dependencies because my work is very tech precautious.

Any help is greatly appreciated.

4 Answers 4

1

First, I want to say that writing plain CGI-scripts in 2017 is a way of a brave person. Your life will be much easier if you would use bottle or flask.

But if you want, here is the way you can start.

  1. python -m http.server --cgi assumes that the cgi-bin is in the current directory. That is you should go to the test directory and start the command from there. And then call http://localhost:8000/cgi-bin/test.py.

  2. Your cgi-script is not correct. The script should be executable. I am not sure if it needs a shebang line in Windows when you run the http.server, but is is required when you run CGI-scripts with Apache web server under Windows. The shebang line starts with #! and then contains the full path to python3 interpreter, and the line should end with \n and not \r\n, otherwise it won't work.

    After that you should output all the HTTP-headers, print a blank line, and output your content. All HTTP-headers should be separated by '\r\n' and not '\n'.

Here is an example CGI-script that works on OS X:

#!/usr/bin/env python3
import sys
sys.stdout.write('Status: 200 OK\r\n')
sys.stdout.write('Content-type: text/html;charset="utf-8"\r\n\r\n')

print('<h1>Hello, world!</h1>')    
Sign up to request clarification or add additional context in comments.

1 Comment

http.server doesn't assume cgi-bin is the current directory. It rather assumes that the CGI script will be put in a subfolder named /cgi-bin (or /htbin according to the docs). This is why he either has to move his CGI scipts in a /cgi-bin subfolder or add /test in the cgi_directories variable.
1

According to the http.server documentation:

This defaults to ['/cgi-bin', '/htbin'] and describes directories to treat as containing CGI scripts.

So, http.server is expecting to find CGI scripts in a subfolder named /cgi-bin ot /htbin in your python.exe folder. If you really want to use the test folder, then change the cgi-directories variable in the http/server.py script.

Comments

0

I think the first line of the script needs to specify the interpreter path. Some examples are:

#!/usr/bin/python
#!/usr/bin/python2.3
#!/usr/bin/python2.4
#!c:\Python24\python.exe
#!c:\Python25\python.exe

See if it helps.

5 Comments

What do you mean by this?
So like, find where my Python is located and then write that up top?
So, I added the path of my Python to the code, but I receive an error message as so: Error response Error code: 404 Message: File not found. Error code explanation: HTTPStatus.NOT_FOUND - Nothing matches the given URI. in my browser.
This is my path: #!C:\Users\BregmanM\AppData\Local\Programs\Python\Python36-32\python.exe
Now I reran it and it's still just outputting the exact code on file.
0

Try to remove the extension if you are in a macos environment, so it is handled as an executable file instead of a script. Worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.