Change language

HttpServer as a simple HTTP server in Python

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

Previously in Python 2.7, this module was called HttpServer . But in Python3 this module has been merged with the http.server module. Let’s get started and run our own Http server.

 Http Server



Importing the httpserver module

This module is part of the standard library, so installing you do not need to write it. To import this module, simply use the following statement:

 import http.server 

You are now ready to start the server. Now let’s write some code for serving files.

Starting an HTTP server

If you just want to share your files and directories with another user, you can start the server directly using Python.

Go to any directory you want to share and start the server from there using:

 python -m http.server 9000 

Here we run our local Http server on port 9000.

Connecting

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

  1. Log in to the server and find the server’s IP address using arp -a on Windows or ip -a | grep inet on Linux.
  2. On the remote client, just type http: // IP_ADDRESS: 9000 / into your browser.

Exit



 Basic Http Server

Pay note that you can view the server files or even upload them to the client machine.

 file list

Start HttpServer, which serves a custom index.html file

Although the default server is handy for direct file sharing, you can configure the server by running a separate file.

For example, we’ll run our own Http server that uses http.server and socketserver for TCP communication.

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

 # server.py import http.server # Our http server handler for http requests import soc ketserver # 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 name it like server.py , you can start the http server using:

 python server.py 

 "home

Since we have defined our custom do_GET () function, we can serve the HTML file of the home page using our server, which in this case is index.html. Also, if the server is running on your system, you can directly access the server using localhost: "portnumber" instead of using IP.



Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically