Network Programming Python - HTTP Server - GeeksforGeeks (2024)

Improve

Improve

Like Article

Like

Save

Report

HTTP Web Server is simply a process which runs on a machine and listens for incoming HTTP Requests by a specific IP and Port number, and then sends back a response for the request.

Python has a built-in webserver provided by its standard library, can be called for simple client-server communication. The http.server and socketserver are the two main functions used to create a web server. Port number can be defined manually in the program which is used to access the webserver. This web server is functional for many types of file formats, but it is not fully featured. A simple static HTML files can easily be parsed and served by responding with the required response code. A basic implementation of python HTTP web server is given below :

Sample HTML File :

<html>

<head>

<title>Python HTTP Server</title>

</head>

<body>

<h1>Simple HTTP Server</h1>

<p>Congratulations! The HTTP Server is working!

Welcome to GeeksForGeeks</p>

</body>

</html>

Python3

# Import libraries

import http.server

import socketserver

# Defining PORT number

PORT = 8080

# Creating handle

Handler = http.server.SimpleHTTPRequestHandler

# Creating TCPServer

http = socketserver.TCPServer(("", PORT), Handler)

# Getting logs

print("serving at port", PORT)

http.serve_forever()

Output:

serving at port 8080127.0.0.1 - - [17/Oct/2020 00:31:27] "GET / HTTP/1.1" 200 -

Network Programming Python - HTTP Server - GeeksforGeeks (1)

Execute the above program in the directory where the HTML File is saved. After creating the Web Server open the Web Browser and type http://localhost:8080/ in the URL.

Here, SimpleHTTPRequestHandler is used to create a custom handler. TCPServer tells the server to send and receive messages. To invoke TCPServer, two arguments are required the TCP Address i.e. IP and PORT, and second is the handler. TCP Address is a tuple consisting of IP address and PORT number. Serve_forever() is a function of the TCPServer instance that starts the server and listens and responds to the incoming requests.

The localhost is the host machine(in our case the system we are using), used to access the network services using the loopback network interface.

If the python program is to be used only as localhost serving, the below program is used for that purpose :

Python3

# Import libraries

import sys

import http.server

import socketserver

# Creating handle

HandlerClass = http.server.SimpleHTTPRequestHandler

# Creating Server

ServerClass = http.server.HTTPServer

# Defining protocol

Protocol = "HTTP/1.0"

# Setting TCP Address

if sys.argv[1:]:

port = int(sys.argv[1])

else:

port = 8000

server_address = ('127.0.0.1', port)

# invoking server

HandlerClass.protocol_version = Protocol

http = ServerClass(server_address, HandlerClass)

# Getting logs

sa = http.socket.getsockname()

print("Serving HTTP on", sa[0], "port", sa[1], "...")

http.serve_forever()

Output:

Network Programming Python - HTTP Server - GeeksforGeeks (2)



Last Updated : 24 Oct, 2020

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Network Programming Python - HTTP Server - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6402

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.