Yes, Python is commonly used for web servers through frameworks like Flask and Django.
Key takeaways:
There are multiple methods to serve web pages in Python:
Using Flask for dynamic web applications.
Utilizing Python’s built-in HTTP server module for simple static pages.
Using Flask:
Install Flask using the command
pip install flask
.Create a
templates
directory to hold HTML files.Create an
app.py
file to manage the application, specifying the host and port.Start the Flask application with the command
python3 app.py
.The setup typically includes:
index.html
: The HTML file that serves as the web page.
script.sh
: A script file with commands to run the application.
To serve a web application using Python, you can use a web framework like Flask or Django. Flask is a lightweight framework, while Django is more comprehensive and suitable for larger projects. There are multiple ways to serve a web page using Python, which includes the following:
Serve the web page using Flask.
Serve the web page using Python's built-in HTTP server module.
We will explore both of these options in detail with code examples.
This section will serve as a web page for a Flask application. To complete this, perform the following steps:
pip install flask
Note: Flask has been installed for you.
templates
to store the HTML
files.app.py
file that will be used to render the HTML
file. This file will contain the information regarding the host and port number of the application.python3 app.py
Below is the working example code, which includes the following:
templates/index.html
: The web page we wanted to serve.
app.py
: The main file contains the code to render the HTML
file as a web application.
script.sh
: A script file, that contains the commands to run the application.
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * {box-sizing: border-box;} body { margin: 0; font-family: Arial, Helvetica, sans-serif; } .header { overflow: hidden; background-color: #f1f1f1; padding: 20px 10px; } .header a { float: left; color: black; text-align: center; padding: 12px; text-decoration: none; font-size: 18px; line-height: 25px; border-radius: 4px; } .header a.logo { font-size: 25px; font-weight: bold; } .header a:hover { background-color: #ddd; color: black; } .header a.active { background-color: dodgerblue; color: white; } .header-right { float: right; } @media screen and (max-width: 500px) { .header a { float: none; display: block; text-align: left; } .header-right { float: none; } } </style> </head> <body> <div class="header"> <a href="#default" class="logo">Welcome to Educative</a> </div> <div style="padding-left:20px"> <h1>Application is served </h1> </div> </body> </html>
In this section, we'll serve the web page without using any framework like Flask by using built-in http-server
module directly from the command line.
To complete this task, perform the following steps:
index.html
file.python3 -m http.server <port>
Note: This command will start a server and serve the web page on
localhost:8080
.
Following is the code example where we have the following files:
index.html
: The web page we wanted to serve.script.sh
: The script file contains the command to start the server.#!/bin/bash cd /usercode && python3 -m http.server 8080
In conclusion, Python offers multiple ways to serve web applications, with Flask providing a lightweight, flexible framework suitable for small to medium projects, while the built-in HTTP server module allows for quick, framework-free deployment.
Haven’t found what you were looking for? Contact Us
Free Resources