dynamic web applications using Python on your Raspberry Pi!
Summary
In this hour, you learned how to use Python to create dynamic web applications. Although it was not
originally intended for use on the web, Python provides many Web features that you can use in your
applications. In this hour, you saw how to use the CGI to run simple Python scripts from a browser,
using the Apache web server. Next, you learned how to incorporate HTML code inside the Python
output to format an application to display in a browser environment. Finally, you walked through
using the cgi module to retrieve data from web forms and process it in your Python programs.
In the next hour, we’ll take a look at how to create some applications using Python on your Raspberry
Pi. The Raspberry Pi is known for its support for high-definition images and video, as well as
support for audio. We’ll walk through writing some applications that can leverage those features!
Q&A
Q. Are there other ways to run Python scripts from the web server, besides using the CGI?
A. Yes, the mod_python and the mod_wsgi Apache plug-in modules provide direct support
for running Python scripts without using the CGI.
Q. What are Python web frameworks?
A. The web frameworks are modules that provide built-in classes for handling many low-level
data retrieval, formatting, and storage features for you. When you use these modules, you can
concentrate more on your web application than on the low-level Python coding. Module
packages such as Django and Pylons are popular with professional Python web developers.
Workshop
Quiz
1. Where should you place your Python scripts so they can be viewed from the Apache web
server?
a. /var/log/apache2
b. /usr/lib/cgi-bin
c. /home/pi
d. /etc/apache2
2. In Python CGI scripts, you can only run modules from the standard Python library. True or
false?
3. What cgi module method should you use to retrieve data from a textarea form element?
Answers
1. b. You must place all web Python scripts in the /usr/lib/cgi-bin folder for security
reasons.
2. False. You can run any module installed in the Raspberry Pi from Python CGI scripts.
3. The getfirst() module retrieves the data passed from a textarea form element.