Building a basic HTTP Server from scratch in Python Last Updated : 15 Mar, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we are going to learn how to set up a simple and local HTTP server using Python. An HTTP server can be very useful for testing Android, PC, or Web apps locally during development. It can also be used to share files between two devices connected over the same LAN or WLAN network. Installation: On the terminal run the following statement: python -m http.server For accessing the server locally we need to visit http://localhost:8000/ or http://127.0.0.1:8000/ Here we can see all the directories of your local storage along with all the data. You can also access an HTML page, It will be rendered by your web browser as you access it. Functions Used: BaseHTTPRequestHandler: It is used to handle the requests that arrive at the server. It doesn't handle the actual HTTP request but handles the Get and Post requests.HTTPServer(server_address,BASE_HTTP_REQUEST_HANDLER()): This is a function that is used for storing the port of the server as well as the name of the server. Step-by-step Approach: We will make a class that handles the server requests.In that class, we are going to create a function that is used for GET_REQUESTS.In that function, we are going to write our HTML code for displaying it on the server.In the end, we are using the HTTPServer() function that is used to run our server. Implementation: Python3 # importing all the functions # from http.server module from http.server import * # creating a class for handling # basic Get and Post Requests class GFG(BaseHTTPRequestHandler): # creating a function for Get Request def do_GET(self): # Success Response --> 200 self.send_response(200) # Type of file that we are using for creating our # web server. self.send_header('content-type', 'text/html') self.end_headers() # what we write in this function it gets visible on our # web-server self.wfile.write('<h1>GFG - (GeeksForGeeks)</h1>'.encode()) # this is the object which take port # number and the server-name # for running the server port = HTTPServer(('', 5555), GFG) # this is used for running our # server as long as we wish # i.e. forever port.serve_forever() How to start our HTTP-Server: Use the following command in the terminal python file_name.py In the browser go to http://localhost:5555/ or http://127.0.0.1:5555/ Create Quiz Comment R rahulmahajann Follow 0 Improve R rahulmahajann Follow 0 Improve Article Tags : Python python-utility Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like