Simple Web Server based in Python

CK CK 3788 
Created at
Updated at  
374 0 0

To deliver content with the simple web server set up using http.server, you can place the content (HTML, CSS, JavaScript files, images, etc.) in the same directory where you run the script. The server will serve files from this directory and its subdirectories.

Here's an example of how to set up and serve some basic content:

  1. Directory Structure:

    코드 복사

    my_web_server/
    ├── index.html
    ├── about.html
    ├── style.css
    └── simple_web_server.py
    
  2. HTML Files:
    • index.html:

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <link rel="stylesheet" href="style.css">
          <title>Home</title>
      </head>
      <body>
          <h1>Welcome to My Web Server</h1>
          <p>This is the home page.</p>
          <a href="about.html">About</a>
      </body>
      </html>
      
    • about.html:

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <link rel="stylesheet" href="style.css">
          <title>About</title>
      </head>
      <body>
          <h1>About This Server</h1>
          <p>This is a simple web server created with Python.</p>
          <a href="index.html">Home</a>
      </body>
      </html>
      
  3. CSS File:
    • style.css:

      body {
          font-family: Arial, sans-serif;
          background-color: #f0f0f0;
          margin: 0;
          padding: 20px;
      }
      
      h1 {
          color: #333;
      }
      
      p {
          font-size: 1.2em;
      }
      
      a {
          display: block;
          margin-top: 20px;
          color: #007bff;
          text-decoration: none;
      }
      
      a:hover {
          text-decoration: underline;
      }
      
  4. Python Server Script:
    • simple_web_server.py (as given before):

      from http.server import SimpleHTTPRequestHandler, HTTPServer
      
      def run_server(port=8000):
          server_address = ('', port)
          httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
          print(f"Starting server on port {port}...")
          httpd.serve_forever()
      
      if __name__ == "__main__":
          run_server()
      

Running the Server

  1. Open a terminal or command prompt.
  2. Navigate to the directory where simple_web_server.py is located:

    cd path/to/my_web_server
    
  3. Run the server script:

    python simple_web_server.py
    
  4. Open a web browser and go to http://localhost:8000. You should see the content from index.html.

Accessing Other Pages

To access the about.html page, navigate to http://localhost:8000/about.html. The server will automatically serve the appropriate content based on the URL path.

This setup allows you to deliver static content (HTML, CSS, JS, images, etc.) from the directory where the server is running.

Tags HTTPServer Python Web Server Simple Python Web Server Facebook X
Comments 0
Similar posts
  1. MFC based World Wide Web HTTP Server Source Code
    460
  1. Coinbase Python API - Basic Example
    462
  2. AWS Lambda Code Example based on Python
    333
  3. Python code using GPU on CentOS with AWS
    1,252
  4. Video Scence Detection with PySceneDetect
    2,323
  5. Python Audio Separation Libraries allowing you to isolate individual tracks
    1,446
  6. Python Version Control for Library Compatibility with pyenv - Downgrading/Upgrading Python Version
    908
  7. Deleting a specific user voice from the original audio track with Python
    549
  8. Creating an MR from the original MP3 audio in Python - Deleting Voice Track from Audio File
    441
  9. Creating mp3 audio file based on the text input with Python
    465
  10. Implementing Text To Speech / Audio with Python3 and pyttsx3
    355
  11. Building Initial Virtual Environment at Python - The first step you should do for Python Programming
    559
  12. Python Simplified Assignment Operators
    533
  13. Trackback module used for Python Threading debugging
    843
  14. Python Flask with simple HTML files to server web pages along with API
    927
  15. Python Flask web server tutorial from the scratch
    623
  16. Thread Implementation in Python
    509
  17. A shell script that activates a Python virtual environment and then runs a specific Python script
    743
  18. Learning about Python Multiprocessing Technique
    592
  19. Simple Example to get the total number of records in BigQuery with Python
    666
  20. Using BigQuery in Python
    412
  21. Python Web Programming based on Django from the Scratch
    374
  22. Flink with Python: Core Concepts, Kafka Integration, and Architectural Trade-offs
    188
  23. How do I determine the client IP type in Python - IPv4 or IPv6
    763
  24. Predicting Buyer Preferences with PyTorch: A Deep Learning Approach
    1,233
  25. PyTorch example to Forcast Stock Price based on 10 days Dataset
    651
  26. Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records
    1,073
  27. Dynamic DNS Made Easy: Building a Python-Based Solution
    796
  28. Milvus & FAISS: Decoding Their ML Synergy
    279
  29. Save clustering result (aka dictionary) in JSON and Reuse loaded result
    378
  30. Forecasting with Linear Regression and KNN Regression in Python
    926
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,015
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    251
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    262