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/ Comment More infoAdvertise with us Next Article Building a basic HTTP Server from scratch in Python R rahulmahajann Follow Improve Article Tags : Python python-utility Practice Tags : python Similar Reads How To Send Files Using Python Built-In Http Server Python's built-in HTTP server offers a straightforward way to share files over a local network or the internet without the need for complex setups. In this tutorial, we'll walk through the step-by-step process of using Python's built-in HTTP server to send files to clients. Setting Up the HTTP Serve 2 min read Creating a Proxy Webserver in Python | Set 1 Socket programming in python is very user friendly as compared to c. The programmer need not worry about minute details regarding sockets. In python, the user has more chance of focusing on the application layer rather than the network layer. In this tutorial we would be developing a simple multi-th 5 min read Creating a Proxy Webserver in Python | Set 2 Prerequisite: Creating a Proxy Webserver in Python - Set1 In this tutorial, a few interesting features are added to make it more useful. Add blacklisting of domains. For Ex. google.com, facebook.com. Create a list of BLACKLIST_DOMAINS in our configuration dict. For now, just ignore/drop the requests 5 min read Build APIs with Falcon in Python In the sector of web development, building a sturdy and green API is vital. APIs (Application Programming Interfaces) act as a bridge between software program structures, allowing for easy verbal exchange and record change. Python, with its flexibility and flexibility, is a popular choice for growin 6 min read Python | Build a REST API using Flask Prerequisite: Introduction to Rest API REST stands for REpresentational State Transfer and is an architectural style used in modern web development. It defines a set or rules/constraints for a web application to send and receive data. In this article, we will build a REST API in Python using the Fla 3 min read Launch an HTTP Server with a Single Line of Python Code Python, with its built-in capabilities, offers a simple and effective way to do this. With just a single line of code, you can launch an HTTP server to serve files from your local directory. This feature can be incredibly handy for developers, educators, and anyone needing a quick and easy way to sh 2 min read How to Deploy Python WSGI Apps Using Gunicorn HTTP Server Behind Nginx This article describes how to deploy a Python WSGI application using Gunicorn and Nginx. Before proceeding with the tutorial, it's a good idea to first familiarize yourself with the terms WSGI, Gunicorn, and Nginx. Web Server Gateway Interface or WSGI is a specification that describes how a web serv 5 min read How to Get Data from API in Python Flask In modern web development, APIs (Application Programming Interfaces) play a crucial role in enabling the interaction between different software systems. Flask, a lightweight WSGI web application framework in Python, provides a simple and flexible way to create APIs. In this article, we'll explore ho 2 min read Python - Binding and Listening with Sockets Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server. 3 min read Building CLI to check status of URL using Python In this article, we will build a CLI(command-line interface) program to verify the status of a URL using Python. The python CLI takes one or more URLs as arguments and checks whether the URL is accessible (or)not. Stepwise ImplementationStep 1: Setting up files and Installing requirements First, cr 4 min read Like