Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4490

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T12:28:09+00:00 2024-11-27T12:28:09+00:00

Building a Simple Web Application with Python

  • 61k

Python is widely used for web development because of its simplicity, versatility, readability, and strong community support. In this article, you will learn the following:

  1. Creating a basic web server using the http.server Python module.
  2. How to handle HTTP requests and responses.
  3. How to serve static content.

Before you start, you should have a basic knowledge of Python language and web development.

Python has a module called http.server that lets you create a server with minimal effort. You can initialize an HTTP server instance by specifying the server address and port. Additionally, you can define a custom request handler to process incoming requests.

Let's begin by writing the code.

To start, open your preferred code editor and create a new folder. Within this folder, create two files: one for the server (simple-web-server.py) and another for static content (index.html). Now, you can paste the provided code into the appropriate files.

# simple-web-server.py

  from http.server import BaseHTTPRequestHandler, HTTPServer  # Define the HTTP request handler class class MyRequestHandler(BaseHTTPRequestHandler):     def do_GET(self):         self.send_response(200)         self.send_header('Content-type', 'text/html')         self.end_headers()          # Read the HTML content from the file         with open('index.html', 'rb') as file:             html_content = file.read()          # Set the response body with the HTML content         self.wfile.write(html_content)      def do_POST(self):         content_length_str = self.headers.get('Content-Length')         if content_length_str is None:             self.send_response(400)  # Bad request             self.end_headers()             self.wfile.write(b'Content-Length header is missing')             return          content_length = int(content_length_str)         post_data = self.rfile.read(content_length)         print("Received POST data:", post_data.decode('utf-8'))          # Send a success response         self.send_response(200)         self.send_header('Content-type', 'text/plain')         self.end_headers()         self.wfile.write(b'POST request received successfully')          return      def do_PUT(self):         content_length_str = self.headers.get('Content-Length')         if content_length_str is None:             content_length_str = '0'          content_length = int(content_length_str)         put_data = self.rfile.read(content_length)         print("Received PUT data:", put_data.decode('utf-8'))          # Send a success response         self.send_response(200)         self.send_header('Content-type', 'text/plain')         self.end_headers()         self.wfile.write(b'PUT request received successfully')  # Define the main function to start the server def main():     # Set the server address and port     server_address = ('', 8080)      # Create an instance of the HTTP server     httpd = HTTPServer(server_address, MyRequestHandler)      # Start the server     print('Starting server...')     httpd.serve_forever()  if __name__ == '__main__':     main() 
Enter fullscreen mode Exit fullscreen mode

# index.html

<!DOCTYPE html> <html> <head>     <title>Simple Web Server</title> </head> <body>     <h1>Welcome to the Simple Web Server</h1>     <p>This basic HTML page is served by a simple web server.</p> </body> </html>  
Enter fullscreen mode Exit fullscreen mode

Let's understand how the code functions:

from http.server import BaseHTTPRequestHandler, HTTPServer

The code starts by importing two modules: BaseHTTPRequestHandler and HTTPServer from the http.server.

The purpose of importing BaseHTTPRequestHandler is to handle the incoming HTTP requests on the server. On the other hand, HTTPServer is used to create an instance of the server.

class MyRequestHandler(BaseHTTPRequestHandler):

Next, we have a class called MyRequestHandler that is created by inheriting from BaseHTTPRequestHandler. This custom request handler class allows us to handle different request methods like GET, POST, and so on.

 def do_GET(self):         self.send_response(200)         self.send_header('Content-type', 'text/html')         self.end_headers()          # Read the HTML content from the file         with open('index.html', 'rb') as file:             html_content = file.read()          # Set the response body with the HTML content         self.wfile.write(html_content) 
Enter fullscreen mode Exit fullscreen mode

Next, In the MyRequestHandler class, we define the function do_GET to handle GET requests. Firstly, we send an HTTP response with the status code 200 (OK). Then, we set the content-type header to text/html and end the response headers. Next, we open the file index.html in binary mode and read its content into the variable html_content. Finally, we send the html_content as the response body to the client.

To see the expected output, you can run the code and visit http://127.0.0.1:8080/ in your web browser or use a tool like curl.

# browser

serving static file

# using curl

serving static file

The above demonstrates the get request process

   def do_POST(self):         content_length_str = self.headers.get('Content-Length')         if content_length_str is None:             self.send_response(400)  # Bad request             self.end_headers()             self.wfile.write(b'Content-Length header is missing')             return          content_length = int(content_length_str)         post_data = self.rfile.read(content_length)         print("Received POST data:", post_data.decode('utf-8'))          # Send a success response         self.send_response(200)         self.send_header('Content-type', 'text/plain')         self.end_headers()         self.wfile.write(b'POST request received successfully') 
Enter fullscreen mode Exit fullscreen mode

Here we define the do_POST method within the MyRequestHandler class to handle POST requests. Then, we retrieve the value of the Content-Length header from the HTTP request and store it in a variable called content_length_str. If the Content-Length header is missing, we send a 400 Bad Request response with a message stating that the header is missing. After that, the code reads the POST data from the request body, using the content length specified in the header. It then decodes the received data from bytes to a UTF-8 encoded string and prints it to the console. Finally, we send a 200 OK response with a text/plain content type, along with a message indicating that the POST request was received successfully.
To test the POST request, we can use the command curl -X POST -H "Content-Length: 0" http://localhost:8080.

POST request

def do_PUT(self):         content_length_str = self.headers.get('Content-Length')         if content_length_str is None:             content_length_str = '0'          content_length = int(content_length_str)         put_data = self.rfile.read(content_length)         print("Received PUT data:", put_data.decode('utf-8'))          # Send a success response         self.send_response(200)         self.send_header('Content-type', 'text/plain')         self.end_headers()         self.wfile.write(b'PUT request received successfully')  
Enter fullscreen mode Exit fullscreen mode

For do_PUT is similar to do_POST, but it handles PUT requests. To test for the PUT request we will use curl -X PUT http://localhost:8080

PUT request

# Define the main function to start the server def main():     # Set the server address and port     server_address = ('', 8080)      # Create an instance of the HTTP server     httpd = HTTPServer(server_address, MyRequestHandler)      # Start the server     print('Starting server...')     httpd.serve_forever()  if __name__ == '__main__':     main() 
Enter fullscreen mode Exit fullscreen mode

The last part of the code defines the function that starts the server.

Conclusion:

Creating a basic web application with Python is an exciting adventure that highlights the language's strengths in web development. Throughout this process, we build a straightforward web server, manage HTTP requests and responses, and deliver static content. These tasks establish the groundwork for developing more advanced web applications.

References:

  • Python Documentation:

  • Mozilla Developer Network (MDN) – HTTP Overview:

beginnerspythontutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.