First, Python is a completely different language from PHP. Although the PHP documentation says that this language is general purpose, it is undeniable that it is highly related to developing 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, developing web applications done purely with Python is a quite complex task (as opposed to its simple).
Having made the introduction, my answer to your question is nay, 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 scripts in Python.
Following his approach, and, as I suggested in the comments, you can run scripts in Python 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.
- Inside 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 file permissions 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 you’re done.
Note that, in this approach, it is necessary to define the Content-type header because, otherwise, the browser might not understand and you will download the script instead of running it. The same goes for the shebang on the first line.
Note that with this same approach, you can also run scripts in PHP, just by changing the shebang.
Another possibility is, instead of creating a root directory to run the Python server, use Apache’s own cgi-bin folder (if you’re using it and it’s configured to handle CGI). That might make things a little simpler. As said in the commentary, you can use the cgi and cgitb library, which can simplify your life a little.
But anyway, my point is, you’re hardly going to create a web application with Python using these approaches. Frameworks like Django and Flask are there for simplify web development. In your specific case, doing a hello world with Flask would be much more simple:
- 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 ready.
That way you didn’t have to set up a directory structure, didn’t have to deal with shebangs, didn’t have to change file permissions and didn’t have to deal with HTTP headers.
Okay, it’s 10 lines of Python, with Developer and everything, but try testing a POST using only Python and CGI and then testing a POST with Flask. I say this because I believe you will not test only "echo hello Worlds"...
The title question to have a little bit to do with the explanation of the question. Or there are two separate questions there. The answer to the title is: yes, it can. The last question is whether Python has a built-in server? This is really what you want to know?
– Maniero
More or less @bigown. I want to know if you have a
hello_world.py
and run it through the command line on a server (python itself) and appear there in a localhost of life the result (in the browser). PHP type, check?– Wallace Maxters
How do you do it then, @bigown? In Python you can "write something to print in the browser"?
– Wallace Maxters
I don’t have much knowledge but I know that directly does not have, need extra tool. I’m reading about it now, maybe it’s what you want: http://waitress.readthedocs.org/en/latest/
– Maniero
In terms of web tools, what I can best recommend, what I’ve been experiencing, are languages, Apis, and frameworks, like
HTML5
;PHP
;CSS3
;MYSQL
;NOSQL
(ex.:MONGODB
);NODE.JS
;ANGULAR.JS
;RUBY ON RAILS
; andSYCL
, by degree of ascending difficulty. If you are interested in computer graphics, you can also choose tools such asWEBGL
. Of all these tools, what comes closer to its purpose at the level of programming paradigm, and software architecture is perhaps NODE, theANGULAR
, and theRUBY ON RAILS
. Once again, it’s useless to try to reinvent– Tiago Morais Morgado
Why not use flask or Django? By the way, take a look at the CGI library.
– ppalacios
As stated in the question itself: I want something simple
– Wallace Maxters
@Pablopalaces really, the "I want something simple" can fit very well with the use of the
Flask
.– Wallace Maxters