In the directory with scripts I start the server in the following way:

python -m http.server 8000

the server starts normally but instead displays the script code. What should I do to see the output of the print function only?

enter image description here

  • Aside: instead of CGI it is convenient to use flask, bottle libraries (or dozens of other libraries). - jfs
  • @jfs is the answer for linux, there is no root directory in windows / - perfect
  • The root directory is not used in the example. The example works regardless of the system. (cgi-bin is a relative path) - jfs
  • @jfs relative paths start with a dot, not a slash - perfect

2 answers 2

Run python -m http.server --cgi

Create a cgi-bin

Create a file \cgi-bin\hello.py

 #!/usr/bin/env python print("Content-type: text/html") print() print("<h1>Hello world!</h1>") 

Here is the article in addition https://pythonworld.ru/web/cgi-1.html

Checked in Windows and Linux on python 3

  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash

So it happened:

we have scripts (we create ourselves):

C: \ python \ srv.py

 from http.server import HTTPServer, CGIHTTPRequestHandler server_address = ("", 8000) httpd = HTTPServer(server_address, CGIHTTPRequestHandler) httpd.serve_forever() 

C: \ python \ cgi-bin \ hello.py

 #!/usr/bin/env python print("Content-type: text/html") print() print("<h1>Hello world!</h1>") 

run the srv.py script

go to the browser by reference: http: // localhost: 8000 / cgi-bin / hello.py

EVERYTHING

and note just follow the link to the cgi-bin directory without specifying the file you will fail. the server just curses it.

  • Instead of srv.py , you can python3 -m http.server 8000 --cgi . - jfs
  • @jfs I did not find such an ekzeshnik in the installation directory, in Linux it may be. - perfect
  • Are you really so literal instructions needed that you can not independently run Python 3 on your system. If the python3 command does not start Python 3 with you, then try the py -3 command or check which version just python -V runs python -V . Such elementary adaptations you must do yourself. - jfs
  • @jfs and who said that I can not run the python? - perfect
  • I gave you the command: python3 -m http.server ... You answered: "I did not find such an ekzeshnik in the installation directory, in Linux it may be" If you are able to run a python, then you should not have any problems to adapt and run: python3 -m http.server .. command for your system. - jfs