A Python web framework called Flask can be used as a Python capable web server. We use pip to install Python packages such as Flask. Install Flask with its dependencies:
$ sudo pip install flask
To test the installation, create a new file called hello.py with the code from below - you can omit the comments after #. The block of code that's most important is the one that contains the string "Hello World!".
from flask import Flask app = Flask(__name__) # Create a Flask object Flask @app.route("/") # Run the code below when someone accesses the root file of the web server def hello(): return "Hello World!" # Send text to the browser if __name__ == "__main__": # Things to do if this script is run from console app.run(host='0.0.0.0', port=80, debug=True) # Set the server to listen to port 80 and report errors
Start the server - it remains running until you stop it by pressing <ctrl>-c:
$ sudo python hello.py
Open browser in a PC and navigate to your Raspberry's IP address. "Hello world!" will appear on the page.