I am trying to learn python (coming from PHP), and want to set up the simplest web server so I can start coding.
I found the integrated HTTP server, so I figured it should be the easiest way.
root@ubuntu:/var/py# python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
The webserver is working, accessing http://test.dev:8000/test.py
(thanks /etc/hosts
) works - but shows me the contents of the file ( print('Hello world!');
), without interpreting it.
How to properly set up the server / interpretor ?
A webserver in Python can be setup in two ways. Python supports a webserver out of the box. You can start a web server with a one liner. But you can also create a custom web server which has unique functionality.
The SimpleHTTPServer module is a Python module that enables a developer to lay the foundation for developing a web server. However, as sysadmins, we can use the module to serve files from a directory. The module loads and serves any files within the directory on port 8000 by default.
Python standard library comes with a in-built webserver which can be invoked for simple web client server communication. The port number can be assigned programmatically and the web server is accessed through this port.
A webserver in Python can be setup in two ways. Python supports a webserver out of the box. You can start a web server with a one liner. But you can also create a custom web server which has unique functionality. In this article you’ll learn how to do that. The web server in this example can be accessed on your local network only.
Python supports a webserver out of the box. You can start a web server with a one liner. But you can also create a custom web server which has unique functionality. In this article you’ll learn how to do that. The web server in this example can be accessed on your local network only.
The http.server (or SimpleHTTPServer for Python 2) module in Python is handy, but it’s only a static file server; it can’t run code written in Python, PHP, or JavaScript. You’ll need something extra to deal with them, and just what you’ll need depends on the server-side language you’re using.
Python simply allow you to access your desktop/server contents via a web browser. This method is not recommended for production use. Use it within trusted home networks. Also, don't forget to set proper permissions to avoid data loss and misuse.
Python is for writing programs in general, not only web-sites.
SimpleHTTPServer
is really just a trivial HTTP server, it serves files. It doesn't try to interpret code.
If you want to do web-development with Python, you should be using a web framework. web.py
is a good choice, you can check its tutorial. Another option is Django, which also has a brilliant tutorial.
Both of them have simple development servers built in, so you'll be able to start experimenting easily.
If you are just starting out with python I would recommend you start with scripts that can be run from the console using python interpreter. (eg: python run1.py)
Once you have mastered the basics, you can move onto web programming. (I am guessing that you want to try web programming since you mention a web server.) In this case, you have multiple options (all of which work with Apache):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With