Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Using Python HttpServer as a simple HTTP Server

  • Vijaykrishna RamVijaykrishna Ram
  • June 18, 2020
  • Python Modules
Using Python HttpServer as a simple HTTP Server

Hello everyone! In today’s article, we’ll take a look at using Python HttpServer.

This module serves as a very quick and easy way to start a local Http Server on your network.

Earlier, in Python 2.7, this module was called HttpServer. But with Python3, this module has been merged into the http.server module.

Let’s get started, and run our own Http Server!


Importing Python httpserver Module

This module comes as part of the standard library, so there’s no need to pip install it!

To import this module, simply use the below statement:

import http.server

Now you’re all set to run the server. Let’s now write a bit of code to serve the files.

Running our Http Server

If you simply want to share your files and directories to another user, you can directly run the server using Python.

Go to whatever directory you wish to share, and run the server from there, using:

python -m http.server 9000

Here, we start our local Http Server at port 9000.

Connecting to the Http Server

Now, to connect to the local server, you must do the following steps:

  1. Go to the server machine, and find out the server IP Address using arp -a on Windows or ip -a | grep inet on Linux.
  2. From the remote client, simply type http://IP_ADDRESS:9000/ on your browser and see the magic!

Output

Http Server Basic
Http Server Basic

Note that you can look at the server files, or even download it to the client machine!

Python Http Server File
Python Http Server File

Running a Python HttpServer that serves a custom index.html file

While the default server is a convenience for directly sharing files, you can customize the behavior of the server, by running a separate file.

For example, we’ll be running a custom Http Server which uses http.server and socketserver for TCP Communication.

The MyHttpRequestHandler calls do_GET() method to serve the request. To serve a custom file for the request, we can override the function by simply defining another do_GET() method that returns a different value.

# server.py
import http.server # Our http server handler for http requests
import socketserver # Establish the TCP Socket connections

PORT = 9000

class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.path = 'index.html'
        return http.server.SimpleHTTPRequestHandler.do_GET(self)

Handler = MyHttpRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("Http Server Serving at port", PORT)
    httpd.serve_forever()

If you call this as server.py, you can run the http server using:

python server.py
Custom homepage Python http server
Custom Homepage for Python http server

Because we defined our custom do_GET() function, we can serve a homepage HTML file using our server which is index.html in this case. Also if the server is running on your system, you can directly access the server using localhost:<portnumber> instead of using the IP.


Conclusion

In this article, we learned how to set up a simple Http server in Python, using the http.server module. We also learned how to define the do_GET() method to serve custom files when we make a request to our server.

References

  • Python Documentation on Http Server

Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Python program to convert Kilometers to Miles
Previous Post Python program to convert Kilometers to Miles
Next Post Using Selenium in Python to Automate Facebook Login
Using Selenium in Python to Automate Facebook Login

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026
cv2.imread: Load Images with OpenCV

cv2.imread: Load Images with OpenCV

  • November 30, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.