Python is widely used for developing large-scale web applications that are not possible to build using .NET and PHP.
Python supports features that are executed with different frameworks such as Django, Flask, Pyramid, and CherryPy commonly used in sites such Spotify and Mozilla.
You’ll also need the source files in the directory Chapter11.
Web Servers
Most Python web applications are executed on a web server through an interface called WSGI (web server gateway interface ). Other Python scripts are executed through CGI (common gateway interface ).
For this section, you’ll need access to a web server with Python support.
There is a free web server available from Aprelium called Abyss Web Server X1 that you can install on your computer to develop and test websites:
Install the Web Server
- 1.
Open the directory where you have saved the software package.
- 2.
Double-click the software package icon.
- 3.
Deselect components you do not want to install. Auto start enables Abyss Web Server auto starting when a Windows session starts – deselect this. Start Menu Shortcuts enables adding Abyss Web Server shortcuts in the Start Menu. Documentation installs help files.
- 4.
Click Next.
- 5.
Choose a directory where you want to install Abyss Web Server files. From now on, <Abyss Web Server directory> will refer to this directory.
- 6.
Click Install.
Set Up Python Support
- 1.
Open the Abyss Web Server’s Console. Make sure the web server is running, then open your web browser, then enter the following in the address field at the top: 127.0.0.1:9999.
Type in the admin password you created when you installed the server.
- 2.
In the Hosts table, click Configure in the row corresponding to the host to which you want to add Python support.
- 3.
Select Scripting Parameters.
- 4.
Check Enable Scripts Execution.
- 5.
Click Add in the Interpreters table.
- 6.
Set Interface to CGI/ISAPI.
- 7.
In the Interpreter field, click Browse..., go to the directory where you have installed Python, and click on python.exe.
- 8.
Check Use the associated extensions to automatically update the Script Paths .
- 9.
Click Add in the Associated Extensions table.
- 10.
Enter py in the Extension field and click OK.
- 11.
Click OK on interpreters screen.
- 12.
Click OK in the Scripting Parameters screen.
- 13.
Select Index Files.
- 14.
Click Add in the Index Files table.
- 15.
Enter index.pj in the File Name field, click OK. Then click OK in the index files screen.
- 16.
Click Restart to restart the server.
Upload your scripts to your public_html directory on the server; then on your computer, open a web browser and enter the URL to your script:
http://server-name/script-name.py
For example:
This is the first line of your script.
Executing a Script
Let’s take a look at an example. Have a look at script.py. Here, we’ve written a script to output a simple HTML page.
This page simply outputs the heading “It works!!.” Upload the script into your public_html directory on your web server, and then navigate to the script URL using your web browser on your computer.
In our lab URL , this would be
Let’s take a look at a practical example. Here, we’re creating a simple contact form . The user is presented with an HTML form that asks for their name, email address, and a message.
When the user clicks the “Submit” button, the HTML form calls a Python script called contactus.py .
The Python script processes the data passed from the HTML form and stores it in a form object.
We can then get the values passed from the HTML form and store them in this object.
The Python script generates another HTML page using print statements for the response to the user.
You can see the output in the following browser.
Python Web Frameworks
If you are using Python in web development, you’ll more than likely be using a Python web framework rather than the old CGI we looked at in the previous section.
A Python web framework is a collection of tools, libraries, and technologies that allow you to build a web application.
One example of a Python web framework is Django (pronounced “Jango”).
Another example is Flask . Let’s take a look at how to create a simple web app using this framework.
Let’s create an app. First thing we need to do is create our main program. To do this, we create a new file called app.py . We’ve included all these files in the Flask directory in the resource files.
Here, we’ve imported our Flask module.
So our first route is the root of our website and is usually the index page. We use @app.route('/') to determine this.
def index() is the name you give to the route defined earlier. This one is called index, because it’s the index (or home page) of the website.
host='0.0.0.0' means the app is accessible from any machine on the network.
To run and test the app , we need to open it using the development environment. This is a simple web server that allows you to open the app in a web browser for testing.
To do this, open your app directory in the command prompt. In this particular example, the app files are in OneDrive/Documents/Flask.
Once you press Enter, the server will start.
You can open the app in a web browser. On your own workstation, you can use localhost: 5000 :
To add another page, add another route.
Now that we have our base app, we can develop web pages for the app to call. These web pages are called templates and we store these in a template directory. Here’s a simple HTML page we’ve created and saved in the templates directory.
Let’s call our HTML page from our app. We can use the render_ template() function .
When we view the page in a browser, we’ll see a rendered version of the HTML page.
You can pass variables to your HTML templates. To do this, embed the variable using {{variable-name}} in your HTML.
Let’s take a look at an example. Open the file app3.py and index2.html .
Here , we’ve passed the title and price as variables to the HTML template.
You can add images to your templates using HTML and CSS code. You can also embed Python code.
Summary
Django, Flask, Pyramid, and CherryPy are common Python web development frameworks.
Most Python web applications are executed on a web server through an interface called WSGI (web server gateway interface).