In this post, we will see how we can create a http webserver using Python 3. There are so many applications for such servers. One of them is the quick way to share your files to someone, or host your website or blog. You can also use paramiko module in Python to securely send files from one computer to another.
If you host your website or blog locally then you don’t need to pay hosting fee to any agent and everything will be in your reach. However, there are other benefits of hosting your website through a good web-hosting platform. I will not go into that details in this post.
The module we will we using for hosting a http server comes by default on your Python3 installation -
http.server. It is also super easy to run. Let us go through two examples.
Host your files on the http server
Let us create a directory called
mywebserver. This will serve as our root of the server. THen we create two more sub-directories inside
mkdir importantFiles notSoImportantFiles
Then we create some files in each subdirectories:
Now, we have enough to serve our server. So, let’s not delay anymore and start our server.
python -m http.server
And, we have our server serving at the
ip_address:port. To check the ip address of your system, you can type:
That’s it. It is as easy as this to host your personal web server using Python. Please note that in some cases, your server may not be accessible from the remote computer. This may probably be because of your system’s firewall. You can stop the firewall temporarily using:
sudo systemctl stop firewalld
Host a blog
Now, we will create a very simple blog, and host it on our personal server. I will copy the html and css codes from the w3schools.com.
Now open the
index.html page and copy and paste the codes from w3schools.com.
Next, you can start your server.
python -m http.server
Disclaimer of liability
The information provided by the Earth Inversion is made available for educational purposes only.
Whilst we endeavor to keep the information up-to-date and correct. Earth Inversion makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services or related graphics content on the website for any purpose.
UNDER NO CIRCUMSTANCE SHALL WE HAVE ANY LIABILITY TO YOU FOR ANY LOSS OR DAMAGE OF ANY KIND INCURRED AS A RESULT OF THE USE OF THE SITE OR RELIANCE ON ANY INFORMATION PROVIDED ON THE SITE. ANY RELIANCE YOU PLACED ON SUCH MATERIAL IS THEREFORE STRICTLY AT YOUR OWN RISK.