First, Python is a totally different language from PHP. Although the PHP documentation says that this language is general purpose, it is undeniable that it is highly related to the development of scripts to be run on a web server.
In contrast, Python is a language that, among many other things, can also be used for web application development. However, due to this plurality of applications, web application development done purely with Python is a rather complex task (as opposed to its simple ).
Made the introduction, my answer to your question is not , it is not possible. The tovmeod response, as far as I know, only allows you to start a web server to manage static files and not to run Python scripts.
Following his approach, and as I suggested in the comments, you can run Python scripts as if it were a CGI script. So to make your simple "hello world" in Python you need:
Create a directory to serve your files.
Within this directory, create a subdirectory called cgi-bin
There, create a python module called hello.py with the following content
#!/usr/bin/env python
print("""Content-type: text/html
Hello world from Python!""")
Change the permissions of the file to be executable
Start the server with the option to serve CGI scripts:
python -m http.server --cgi 8000
Visit localhost: 8000 / cgi-bin / hello.py and that's it.
Note that in this approach you need to define the Content-type header, otherwise the browser may not understand and you will download the script instead of running it. The same goes for the shebang in the first line.
Note that with this same approach, you can also run PHP scripts by simply switching the shebang.
Another possibility is to use Apache's own cgi-bin folder instead of creating a root directory to run the Python server (if you are using it and it is configured to handle CGI). This can make things a bit simpler. As I said in the comment, you can use the cgi and cgitb library, which can somewhat uncomplicate your life.
But anyway, my point is, you will hardly create a web application with Python using these approaches. Frameworks like Django and Flask are there to simplify development for the web. In your specific case, doing a hello world with Flask would be much simpler :
Install the flask pip install flask
Create the hello.py file anywhere with the following content:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'hello from flask'
if __name__ == '__main__':
app.run()
Run the script python hello.py
Visit localhost:5000
and that's it.
That way you did not need to set up a directory structure, did not have to deal with shebangs, did not need to change file permissions, and did not have to handle HTTP headers.
Okay, there are 10 Python rows, with decorator and everything, but, try testing a POST using only Python and CGI and then test a POST with Flask. I say this because I believe you will not just test "echo hello worlds" ...