Launch an HTTP Server with a Single Line of Python Code Last Updated : 27 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 share files over a network. Use Cases to Launch An HTTP ServeLocal Development: One of the most common use cases for launching a quick HTTP server is during web development. It allows developers to serve static files (HTML, CSS, JavaScript) and see changes in real time.Testing and Debugging: When testing web applications or debugging issues, serving files locally can help simulate a web server environment without deploying to an actual server.Sharing Files: You can easily share files with others on the same network by running an HTTP server and providing the URL to your colleagues or friends.Below are the Different methods by which you can launch the local server: Python's http.server ModulePython's http.server module is a built-in library that provides basic HTTP server capabilities. It allows you to serve content from a directory over HTTP without needing to install any external libraries or configure complex settings. python -m http.serverBy default, this command will start an HTTP server on port 8000, serving the contents of the current directory. Specifying a Different PortIf you need to run the server on a different port, you can specify the port number as an argument: python -m http.server 8080 This command starts the HTTP server on port 8080. You can now access it via http://localhost:8080. With Python Code Here’s a detailed explanation of each part of the code: Imports: The necessary modules (http.server and socketserver).Configuration: Sets the port number to 9000 and assigns SimpleHTTPRequestHandler as the request handler.Server Setup: Creates a TCP server that listens on all available IP addresses ("") and the specified port (PORT), using SimpleHTTPRequestHandler to handle requests.Running the Server: The server starts running and will continue to run, serving files from the current directory, until it is manually stopped. Python import http.server import socketserver PORT = 9000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer(("", PORT), Handler) as httpd: print(f"Serving at port {PORT}") httpd.serve_forever() Output Comment More infoAdvertise with us Next Article How To Send Files Using Python Built-In Http Server C CoderSaty Follow Improve Article Tags : Python Practice Tags : python Similar Reads Setting up a simple HTTP server using Python 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. Inst 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 Configuring HTTPD server on Docker Container and setting up Python Interpreter In this article, we are going to discuss a step-by-step guide on how to configure the apache web server on top of a docker container, and setting up a Python interpreter. Basically, we are installing the product of apache which is HTTPD on the docker container. What is Docker? In simple words, if I 4 min read 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 Python | Launch a Web Browser using webbrowser module In Python, webbrowser module is a convenient web browser controller. It provides a high-level interface that allows displaying Web-based documents to users. webbrowser can also be used as a CLI tool. It accepts a URL as the argument with the following optional parameters: -n opens the URL in a new 2 min read Access files of a devices in the same network using Python We have so many ways to Transfer Files from one computer to another in Linux and Ubuntu but there we have to change the permissions of the Apache Folder and then Share the files. But in windows, we cannot share files using Apache so here we have discussed Python Method to Share files between Systems 2 min read Like