How to serve a web page using Python

Key takeaways:

  1. There are multiple methods to serve web pages in Python:

    1. Using Flask for dynamic web applications.

    2. Utilizing Python’s built-in HTTP server module for simple static pages.

  2. Using Flask:

    1. Install Flask using the command pip install flask.

    2. Create a templates directory to hold HTML files.

    3. Create an app.py file to manage the application, specifying the host and port.

    4. Start the Flask application with the command python3 app.py.

  3. The setup typically includes:

    1. index.html: The HTML file that serves as the web page.

    2. 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:

  1. Serve the web page using Flask.

  2. Serve the web page using Python's built-in HTTP server module.

We will explore both of these options in detail with code examples.

Serve web page with Flask

This section will serve as a web page for a Flask application. To complete this, perform the following steps:

  1. Install Flask using the following command:
    pip install flask
    

    Note: Flask has been installed for you.

  2. Create a directory named templates to store the HTML files.
  3. Create another new 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.
  4. Use the following command to run 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>
Serve web page using Flask

Server web page with HTTP server module

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:

  1. Navigate to the directory where you have the index.html file.
  2. Use the following command to serve the web page:
    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
Serve web page with http-server module

Conclusion

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.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Is Python used for web servers?

Yes, Python is commonly used for web servers through frameworks like Flask and Django.


Is Python easier than PHP?

Many find Python easier to learn due to its clear syntax, but ease of use can vary based on individual experience.


What is HTTP in Python?

HTTP in Python refers to the protocol used for transferring data over the web, which can be handled using libraries like http.client, requests, or web frameworks.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved