How To Send Files Using Python Built-In Http Server
Last Updated :
27 Mar, 2024
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 Server
The first step is to start the HTTP server. Open your terminal or command prompt and navigate to the directory containing the files you want to share. Then, execute the following command:
python -m http.server
Accessing Files
Once the server is running, any files in the current directory can be accessed by clients. Clients can use a web browser or an HTTP client to access the files by navigating to the server's URL followed by the file name.
For example, if the server is running on localhost and port 8000, and there's a file named example.txt in the directory, it can be accessed at http://localhost:8000/example.txt.
Sending a WebPage Using built-in HTTP Server of Python
Below are step-by-step approaches to send a file in Python using a built-in HTTP server:
Step 1: Create an HTML File
Create an HTML file named index.html with the content you want to display. For example
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Webpage</title>
</head>
<body>
<h1>Welcome to My Webpage!</h1>
<p>This is a sample webpage served using Python's built-in HTTP server.</p>
</body>
</html>
Step 2: Start the HTTP Server
Open your terminal or command prompt, navigate to the directory containing the index.html file, and run the following command:
python -m http.server
This command starts the HTTP server on the default port (8000) and serves files from the current directory.
Step 3: Access the Webpage
Open a web browser and navigate to the following URL:
http://localhost:8000/index.html
You should see the contents of the index.html file displayed in the browser, with the title "My Webpage," a heading "Welcome to My Webpage!", and a paragraph with sample text.
Output:

Similar Reads
How to Download and Upload Files in FTP Server using Python? Prerequisite: FTP, ftplib Here, we will learn how to Download and Upload Files in FTP Server Using Python. Before we get started, first we will understand what is FTP. FTP(File Transfer Protocol) File Transfer Protocol(FTP) is an application layer protocol that moves files between local and remote f
3 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 do Cloud File Sharing using Python? In this article, we will see how to share files online using a free, secure platform called GoFile. We can make it available for anyone through the link generated with the help of Python. Gofile is a platform for sharing and storing files. You have no restrictions on how much content you can save or
4 min read
Merge PDF stored in Remote server using Python Prerequisites: Working with PDF files in Python There are many libraries for manipulating PDF files in Python but all are using when that all PDF files already downloaded in your local machine. But what if your target PDF files are in Remote server, you have only URLs of files and no required downlo
2 min read
Building a basic HTTP Server from scratch in 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. Ins
2 min read