Back to Python
2026-01-086 min read

Example Website (Python Programming)

Learn Example Website (Python Programming) step by step with clear examples and exercises.

Title: Example Website (Python Programming)

Why This Matters

Creating a website is an essential skill for anyone interested in showcasing their projects, sharing ideas, or building an online presence. In this tutorial, we'll learn how to create a simple example website using Python, which will serve as a foundation for more complex web development projects. By the end of this lesson, you'll have a better understanding of how to use Python to build and serve static websites.

Prerequisites

Before diving into the core concept, make sure you have:

  1. Basic knowledge of Python programming language (including functions, variables, data structures, and file handling)
  2. Familiarity with directory structure and navigation
  3. Understanding of HTML (HyperText Markup Language) for structuring web pages
  4. A text editor or Integrated Development Environment (IDE) like Visual Studio Code, PyCharm, or Jupyter Notebook to write and run your code
  5. Knowledge of command-line interfaces (CLI) for navigating directories and running Python scripts
  6. Familiarity with web browsers for testing your websites

Core Concept

To create a simple example website using Python, we'll use the built-in http.server module. This module allows you to start a basic web server that serves static files (HTML, CSS, images) from a specified directory.

Step 1: Create your project directory and HTML file

Create a new folder for your project, e.g., example_website. Inside this folder, create an index.html file with the following content:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example Website</title>
</head>
<body>
<h1>Welcome to my Example Website!</h1>
</body>
</html>

Step 2: Install the required Python package (if needed)

If you haven't already, install the http.server module by running the following command in your project directory:

pip install http.server

Step 3: Start the web server

Run the following command in your project directory to start the web server:

python -m http.server

Now, open a web browser and navigate to http://localhost:8000. You should see your example website displayed on the screen.

Worked Example

Let's create a more complex example website with multiple pages. Create a new folder called example_website_2 and follow these steps:

  1. Create an index.html file with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example Website</title>
</head>
<body>
<h1>Welcome to my Example Website!</h1>
<nav>
<a href="/about.html">About Me</a>
<a href="/contact.html">Contact</a>
</nav>
</body>
</html>
  1. Create a new file called about.html with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>About Me</title>
</head>
<body>
<h1>About Me</h1>
<p>This is a brief description about the creator of this website.</p>
</body>
</html>
  1. Create a new file called contact.html with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact</title>
</head>
<body>
<h1>Contact</h1>
<form action="/submit_contact" method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email"><br>
<label for="message">Message:</label><br>
<textarea id="message" name="message"></textarea><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
  1. Create a new Python script called submit_contact.py to handle the form submission:
import cgi
import cgitb

cgitb.enable()

print("Content-Type: text/plain")
print()

form = cgi.FieldStorage()
name = form.getvalue('name')
email = form.getvalue('email')
message = form.getvalue('message')

print(f"Name: {name}")
print(f"Email: {email}")
print(f"Message: {message}")
  1. Run the web server as before:
python -m http.server

Now, open a web browser and navigate to http://localhost:8000. You should see your example website with links to the "About Me" and "Contact" pages. Clicking on the "Contact" link will take you to the new page with a contact form. Submitting the form will send the data to the submit_contact.py script, which will print out the submitted values in the terminal.

Common Mistakes

  1. Forgetting to install the required Python package (http.server) or not running it from the correct project directory
  2. Misconfiguring the HTML file structure or syntax, causing the web server to fail
  3. Failing to restart the web server after making changes to the HTML files or Python scripts
  4. Not handling form submissions correctly in the Python script (e.g., not printing out the submitted values)
  5. Not securing your website by using HTTPS and implementing best practices for user input sanitization and server updates

Practice Questions

  1. Modify the example website to include a navigation bar with links to multiple pages on your example website (e.g., "About Me", "Contact", "Portfolio")
  2. Create a simple login system for your example website using Python and Flask or Django
  3. Integrate a database into your example website to store user information and form submissions
  4. Style your example website using CSS to improve its appearance and make it more visually appealing
  5. Implement additional features such as user registration, password protection, and email notifications for the contact form
  6. Optimize your website for search engines (SEO) by including relevant keywords, meta tags, and sitemaps
  7. Implement a Content Security Policy (CSP) to prevent cross-site scripting (XSS) attacks and improve your website's security
  8. Test your website on various devices and browsers to ensure compatibility and responsiveness
  9. Analyze user behavior and traffic patterns using tools like Google Analytics or Matomo to better understand your audience and make data-driven decisions
  10. Continuously update and maintain your example website to keep it secure, functional, and up-to-date with the latest best practices in web development

FAQ

Q: Why can't I access my example website from other devices on my local network?

A: By default, the web server only listens on localhost, which means it is accessible only from the same machine where it was started. To make your website accessible to other devices, you need to configure port forwarding on your router.

Q: How can I serve dynamic content (e.g., PHP or JavaScript) using Python?

A: For serving dynamic content, you can use a framework like Flask or Django. These frameworks provide additional functionality for handling user input and generating dynamic HTML pages.

Q: Can I host my example website on the internet so that others can access it?

A: Yes, there are many web hosting providers available where you can host your Python-based websites. Some popular options include Heroku, AWS Elastic Beanstalk, and Google App Engine.

Q: How do I secure my example website from unauthorized access or attacks?

A: To secure your website, you should implement best practices such as using HTTPS, sanitizing user input, and keeping your server and dependencies up-to-date. Additionally, consider implementing a Content Security Policy (CSP) to prevent cross-site scripting (XSS) attacks.

Q: What are some other Python web development frameworks I can explore?

A: In addition to Flask and Django, there are several other popular Python web frameworks such as Pyramid, Tornado, and Web2py. Each has its own strengths and weaknesses, so it's worth exploring them to find the one that best suits your needs.

Example Website (Python Programming) | Python | XQA Learn