Python Web Server for your Raspberry Pi

You know some basic Python or you want to learn some and you want to make a website, a web server, a web crawler, etc. – But where do you start? Services like Heliohost or Vlexofree are great for free Python website hosting, but they are not always reliable, have a position queue, or have too much restriction for your project. So you have a Raspberry Pi and Instructables.com and you find my tutorial that describes just what you are looking for. I'll teach you how to set up and use a simple web server framework called Flask along with a basic idea about what you can do with your new Python-based web server.

Step 1: Installing Raspbian

Raspbian is a distribution of Debian, a Linux operating system. Raspbian is the most common OS for your Raspberry Pi.

1) To download Raspbian, go to http://www.raspberrypi.org/downloads/ and download Raspbian via either torrent or direct download.

2) Extract the zip file (Right click & click extract for windows; Right click and click Open with > Archive Utility for mac).

3) Plug in your SD card for your Raspberry Pi into your computer.

4) Copy the Raspbian files to your SD card. I have tried mounting them using Win32DiskImager but I had no success with that.

5) Set up your Raspberry Pi by plugging it into a power source, attaching the Ethernet cable (Internet cable), connecting it to a TV (AV or HDMI; Also an audio cable if you are using AV), attaching a mouse, and attaching a keyboard.

6) Go through the setup process

7) Type ‘startx' and hit Enter if you want to launch the desktop mode (Don't use desktop mode when you are running your website as it will use more power and available resources)

8) If you are in desktop mode, you can test to see if your internet works by opening your browser. If you are still in terminal mode, then you can test your internet by typing ‘ping google.com' and hitting Enter.

This tutorial is more easily accomplished in desktop mode. To do terminal commands in desktop mode, open the terminal via the explorer bar's icon that looks like an empty monitor.Python Web Server for your Raspberry Pi

Step 2: Installing Flask

Pip is a common package installer. We will use this to get Flask. You will need an internet connection.

In the terminal, type the following:

sudo apt-get install python-pip

Hit enter and then type the following:

sudo pip install Flask

Remember, this is case-sensitive!

With this you get a WSGI compliant web framework, a web server, and a templating engine (Jina2 templates).

Step 3: Making Your First Python Web Page

First, make a new folder in /home/pi and call it whatever you want. For this tutorial I will called it ‘website'. Then, make a file called hello.py. You can make a hello.txt and then change the .txt to .py. Open this Python file with one of the text editors that comes with Raspbian.

If you go to the Flask website, as of 3/31/15, you will first see the following example code:

from flask import Flask
app = Flask(__name__)

@app.route(“/”) def hello():

return “Hello World!”

if __name__ == “__main__”:

app.run(host='0.0.0.0′)

So let's go through what this means.

from flask import Flask
The first line imports the Flask library. This allows Python to use commands from the Flask library.

app = Flask(__name__)

This second line gives a name to your application. __name__ is a bland and default name. It only works if you are using the default modules (Libraries). If you want to give your application a custom name, replace __name__ with ‘YourApplicationName' – Of course, change what is in the apostrophes. If you are importing this Python script from another application, you would want to set __name__ to your module (Source file) name. If you still use __name__, then Python will automatically set it to __main__ when it runs.

@app.route(“/”) def hello():

return “Hello World!”

This is the bread and butter of your Python web server. @app.route(“/”) defines where a page is. With only a “/” as the path, “Hello World!” will be returned to the home page whenever someone tries to access it. def hello(): describes what is called a function. Anything that is indented under that function will be part of what happens when someone accesses the homepage. You can't start your function name with a number!

if __name__ == “__main__”:

app.run()

As I said before, __name__ will automatically be read as __main__ when Python runs the script. So it is saying that if __name__ == “__main__” then run the app. Remember that app is set to __name__, which is the application!

Adding more to what is printed to a page

To add more to what is returned when someone accesses a page, you can write entire Python scripts in the def hello(): function! Try starting out with some basic things like math functions and concatenating numbers with strings. Here is a basic example of what you can put in your def hello(): function:

printedText = “Hello World! This is a basic Python script that concatenates the integer “+str(5)+” with the rest of this string!”

printedTextLen = “The length of the previous sentence is “+str(len(printedText))+” characters.”

return printedText, ”
“, printedTextLen

The ”
” is a line break written in HTML format. It would be the same as if you were to hit Enter on your keyboard in a text editor. A web browser will see this as HTML and automatically make a line break.

Step 4: You're done! (Kind of)

You can now start your Python web server. Simply type in the terminal:

cd /home/pi/website

python hello.py

‘cd' means ‘Change Directory'. It will change the terminal directory to /home/pi/website. /website/ is the website folder you created earlier. Using the keyword python in the beginning of a terminal statement automatically switches to the Python interpreter. Saying hello.py after that makes the Python interpreter run hello.py, your hello world script.

Now go to a different computer on the SAME network as the Raspberry Pi and try typing in one of the following into your web address bar:

http://0.0.0.0:5000/

http://raspberrypi:5000/

http://YourRaspberryPi'sIPAddressHere:5000/

The last one is probably going to work. To get your Raspberry Pi's LOCAL IP address, NOT your PUBLIC IP address, you must go into terminal and type in the following:

sudo ifconfig

You will get something returned like what is shown in the above picture. Your local IP address will be what follows inet addr. You can also try logging into your router to find your Raspberry Pi's local IP address.

Step 5: Returning HTML files

So, I'm guessing you're a little overwhelmed about all of the programming but a little underwhelmed about what you get out of it. Guess what, you don't need to write your pages in Python! You can keep it simple and use the Jinja2 template program that already comes with Flask. This will allow you to drag-and-drop your HTML, CSS, and web-scripting files into a folder and you DON'T need to edit them for Python! So let's start off with a basic HTML page with some basic styling.

<html>

<head>

<title>My Python Website!</title>
</head>

<body>

<h1><font size=”6″ color=”red” face=”verdana”>Welcome to my Python-based website!</font></h1>

<p><b><i><font size=”3″ color=”gray” face=”verdana”>This is my first Python-based website that is running on my Raspberry Pi!</font></i></b></p>

</body>

</html>

See how simple HTML is? It is a VERY easy language to learn because it is much like english! So save that as whatever you want, but make sure that it has .html, not .txt. Now make a folder in the same directory as your Python files and call it templates . Make sure that templates is spelled WITHOUT capitalization and that is includes an ‘s' at the end. The exact spelling must be templates . Place your HTML file in the templates folder. Then REPLACE your hello.py file with the following code.

from flask import Flask

from flask import render_template

app = Flask(__name__)

@app.route(‘/')

def mypysite(name=None):

return render_template(‘index.html')

if __name__ == “__main__”:

app.run(‘0.0.0.0')

So go to your terminal and use cd (Change directory) to navigate to your website folder as you did before. Type python hello.py to run your server. Open up the website on a different computer on the SAME network and you will see that index.html will run! index.html is the HTML file I wrote above.Python Web Server for your Raspberry Pi schematic

Step 6: Making it LIVE!

You've made your Python website and it was pretty easy, wasn't it? Well now it is time to go live!

First, log into your router. To do this, do one of the following:

For Windows: Hold the WinKey+R and type in ‘cmd'. Hit enter and you have your command prompt. Type ‘ipconfig' and hit enter. Look for your Default Gateway. Mine is 10.0.0.1, but yours might be something like 192.168.1.1 or 192.168.1.254 .

For Mac: On your mac bar, click the Apple button and then System Preferences. Select Network under Internet and Wireless. Select your connected network. Click the TCP/IP tab and your router's IP will be displayed below.

For linux (Maybe mac): Open terminal and type the following: route -n | grep ‘UG[ \t]' | awk ‘{print $2}' (Or just copy/paste it). It will return ONLY your default gateway (Router IP address). Tell me if I am wrong that this also works for mac.

Now that you have your router's IP address, type it into your browser and hit enter. You will probably get a log in form. Usually, the log in is admin:password or admin:admin. You will need to consult with your network administrator if you are not the administrator. For more information about finding your router's log in information, go to Portforward.com's password list OR look for it on one of the sides of your router.

Once you've logged into your router, try to navigate to your portforwarding section. This will be different for all routers. Portforward.com may also help with this. Once there, you want to click something along the lines of “Add Service”. Below is the information you want to fill out. If you have extra boxes, try leaving them blank or adding what you think is the necessary information. If you don't have to fill out all of the information I provide, then try with what you can.

 

For more detail: Python Web Server for your Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top