Chapter 9 Introduction to Web Development with Python

Chapter 9 Introduction to Web Development with Python#

Web development is a field of computer science that involves the creation and maintenance of websites and web applications. In this post, we will cover some of the fundamental concepts of web development, including client-server architecture, the HTTP protocol, and the technologies used to create websites and web applications, such as HTML, CSS, and JavaScript.

Client-Server Architecture

When you use a web browser to visit a website, your browser is acting as a client, while the server is the computer hosting the website. This client-server architecture is the foundation of the World Wide Web. The client sends a request to the server, which then sends a response back to the client. This exchange of information allows web pages to be displayed in your browser.

HTTP Protocol

The Hypertext Transfer Protocol (HTTP) is the primary protocol used to exchange data on the World Wide Web. When a client sends a request to a server, it sends an HTTP request, which contains information such as the type of request being made (GET, POST, PUT, DELETE, etc.), the resource being requested (e.g., a web page or an image), and any additional data needed to complete the request.

The server responds with an HTTP response, which includes the requested resource, status codes (e.g., 200 OK, 404 Not Found), and any additional data needed to complete the response. Understanding the HTTP protocol is essential for web developers, as it is the foundation of how web pages are requested and delivered.

HTML

Hypertext Markup Language (HTML) is the standard markup language used to create web pages. HTML allows developers to create the structure and content of web pages. HTML uses tags to mark up elements of a web page, such as headings, paragraphs, images, and links. Here’s an example of what HTML code looks like:

<!DOCTYPE html>
<html>
 <head>
 <title>My Website</title>
 </head>
 <body>
 <h1>Welcome to My Website</h1>
 <p>This is a paragraph.</p>
 <img src="myimage.png" alt="My Image">
 <a href="https://www.example.com/">Click here to visit Example.com</a>
 </body>
</html>

CSS

Cascading Style Sheets (CSS) is a stylesheet language used to describe the presentation of a web page. CSS allows developers to control the layout, color, font, and other visual aspects of a web page. CSS is used in conjunction with HTML, allowing developers to create visually appealing and responsive web pages. Here’s an example of what CSS code looks like:

body {
 background-color: #f5f5f5;
 font-family: Arial, sans-serif;
}

h1 {
 color: #333;
 font-size: 32px;
 margin-top: 20px;
}

p {
 color: #666;
 font-size: 16px;
 line-height: 1.5;
 margin-bottom: 20px;
}

img {
 width: 100%;
 max-width: 600px;
 height: auto;
 display: block;
 margin: 0 auto;
}

a {
 color: #0070c0;
 text-decoration: none;
}

JavaScript

JavaScript is a programming language used to create interactive and dynamic web pages. JavaScript can be used to add functionality to web pages, such as responding to user events, manipulating the HTML and CSS of a web page, and sending and receiving data from a server. JavaScript is often used in conjunction with HTML and CSS to create dynamic and responsive web pages.

Python Web Frameworks: An Introduction to Flask, Django, Pyramid, and More

Python is a versatile programming language that can be used for a wide range of applications, including web development. Python web frameworks make it easier to build web applications by providing a set of tools and libraries that handle common tasks such as routing, database access, and user authentication. In this post, we will introduce some of the popular web frameworks in Python, including Flask, Django, Pyramid, and more.

Flask

Flask is a lightweight web framework that is easy to learn and use. It is designed to be simple and flexible, making it a popular choice for building small to medium-sized web applications. Flask uses Jinja2 as its templating engine, allowing developers to easily create HTML templates that can be rendered by the web application. Flask also provides built-in support for unit testing, making it easy to test your application as you develop it.

Django

Django is a full-stack web framework that provides everything you need to build complex web applications. It includes an ORM (Object-Relational Mapping) for working with databases, a templating engine for creating HTML templates, and a built-in administrative interface for managing your application. Django is known for its “batteries included” approach, providing a wide range of built-in functionality that makes it easy to get started with web development in Python.

Pyramid

Pyramid is a flexible web framework that is designed to be easy to use and extend. It is built on top of the WSGI (Web Server Gateway Interface) specification, allowing it to work with a wide range of web servers and tools. Pyramid includes a built-in templating engine, and supports a wide range of databases and data storage options. Pyramid is often used for building large-scale web applications that require a high degree of flexibility and customization.

Bottle

Bottle is a lightweight web framework that is designed to be easy to use and learn. It includes a built-in templating engine, and can be used with a wide range of web servers and tools. Bottle is often used for building small to medium-sized web applications, and is a popular choice for building RESTful APIs (Application Programming Interfaces).

CherryPy

CherryPy is a minimalist web framework that is designed to be fast and easy to use. It includes a built-in web server, and can be used with a wide range of databases and data storage options. CherryPy is often used for building small to medium-sized web applications that require high performance and scalability.

Conclusion

Python web frameworks provide a powerful set of tools for building dynamic and responsive web applications. Whether you are building a small web application or a large-scale enterprise application, there is a Python web framework that can help you get the job done. Flask, Django, Pyramid, Bottle, and CherryPy are just a few of the popular web frameworks in Python, each with its own strengths and weaknesses. By choosing the right framework for your needs, you can build web applications that are fast, reliable, and easy to maintain.

Creating a Simple Web Application using Flask: A Step-by-Step Tutorial

In this tutorial, we will walk through the process of creating a simple web application using Flask, a lightweight web framework in Python. Our application will be able to handle requests, responses, templates, and forms, and will demonstrate some of the key features of Flask.

Step 1: Install Flask

Before we begin, we need to make sure that Flask is installed. Open up a terminal or command prompt, and run the following command:

pip install Flask

This will install Flask and its dependencies.

Step 2: Create a Flask Application

Now that we have Flask installed, we can create a new Flask application. Create a new file called app.py, and add the following code:

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/')
def index():
 return 'Hello, World!'

This code creates a new Flask application, and defines a single route at the root URL (/). When a user visits this URL, the index function is called, which simply returns the string ‘Hello, World!’.

Step 3: Running the Flask Application

To run the Flask application, we need to set the FLASK_APP environment variable to the name of our app.py file, and then run the flask run command. Open up a terminal or command prompt, and run the following commands:

export FLASK_APP=app.py
flask run

This will start the Flask application on your local machine, and you should see a message that says “Running on http://127.0.0.1:5000/”.

Step 4: Adding a Template

Now that we have a basic Flask application up and running, let’s add a template to make it look a bit nicer. Create a new directory called templates, and create a new file called index.html inside this directory. Add the following code to index.html:

<!DOCTYPE html>
<html>
 <head>
 <title>Hello, World!</title>
 </head>
 <body>
 <h1>Hello, World!</h1>
 </body>
</html>

This code defines a simple HTML document with a title and an h1 element that says “Hello, World!”.

Next, update the index function in app.py to render this template instead of returning a string:

@app.route('/')
def index():
 return render_template('index.html')

Now, when a user visits the root URL (/), the index function will render the index.html template, which will display the “Hello, World!” message in a nice HTML page.

Step 5: Adding a Form

Finally, let’s add a form to our Flask application that allows users to enter their name and submit it to the server. Update index.html to include a form:

<!DOCTYPE html>
<html>
 <head>
 <title>Hello, World!</title>
 </head>
 <body>
 <h1>Hello, World!</h1>
 <form method="POST" action="{{ url\_for('hello') }}">
 <label for="name">Enter your name:</label>
 <input type="text" id="name" name="name" required>
 <button type="submit">Say Hello</button>
 </form>
 </body>
</html>

This code adds a simple form with a text input for the user’s name, and a submit button. When the form is submitted, it will send a POST request to the

To run a web application locally, you can use the built-in HTTP server that comes with Python. This server allows you to quickly test your application without the need for a full-blown web server like Apache or Nginx.

Here are the steps to run a web application using the built-in HTTP server in Python:

Step 1: Navigate to the directory containing your web application

First, navigate to the directory containing your web application using the command line. For example, if your web application is in a directory called “myapp”, you would navigate to that directory using the following command:

cd myapp

Step 2: Start the HTTP server

Once you’re in the directory containing your web application, you can start the HTTP server by running the following command:

python -m http.server

This will start the server on port 8000 by default. You can specify a different port by adding the port number to the command, like this:

python -m http.server 8080

This will start the server on port 8080 instead of the default port 8000.

Step 3: Access your web application

Once the server is running, you can access your web application by opening a web browser and navigating to http://localhost:8000 (or the port number you specified in step 2).

If your web application has an index.html file in its root directory, the server will automatically serve that file when you access the root URL (http://localhost:8000). Otherwise, you can access other files in your web application by navigating to their URLs relative to the root URL.

For example, if you have a file called “about.html” in a subdirectory called “pages”, you could access that file by navigating to http://localhost:8000/pages/about.html.

That’s it! With these simple steps, you can quickly run a web application locally using the built-in HTTP server in Python.

You can also use the built-in HTTP server in Python to host static files. Static files include files such as HTML, CSS, JavaScript, images, videos, and audio files that do not change dynamically.

Static Files#

Here are the steps to host static files using the HTTP server in Python:

Step 1: Navigate to the directory containing your static files

First, navigate to the directory containing your static files using the command line. For example, if your static files are in a directory called “myfiles”, you would navigate to that directory using the following command:

cd myfiles

Step 2: Start the HTTP server

Once you’re in the directory containing your static files, you can start the HTTP server by running the following command:

python -m http.server

This will start the server on port 8000 by default. You can specify a different port by adding the port number to the command, like this:

python -m http.server 8080

This will start the server on port 8080 instead of the default port 8000.

Step 3: Access your static files

Once the server is running, you can access your static files by opening a web browser and navigating to http://localhost:8000 (or the port number you specified in step 2).

If your static files are in the root directory, the server will automatically serve the index.html file when you access the root URL (http://localhost:8000). Otherwise, you can access other files in your directory by navigating to their URLs relative to the root URL.

For example, if you have an image file called “image.jpg” in a subdirectory called “images”, you could access that file by navigating to http://localhost:8000/images/image.jpg.

That’s it! With these simple steps, you can quickly host static files using the built-in HTTP server in Python. Note that this server is not suitable for hosting large-scale production applications, but it can be useful for testing and small-scale applications.

Aws Deployment#

Deploying a web application on a cloud platform like Heroku or AWS involves several steps. Here is a general overview of the process:

Step 1: Create an account

Create an account on the cloud platform of your choice. Follow the instructions on the platform’s website to create an account.

Step 2: Install the platform’s command line interface (CLI)

Each cloud platform has a command line interface (CLI) that allows you to interact with the platform from the command line. Install the CLI for the platform you have chosen.

Step 3: Create a new application

Create a new application on the cloud platform by following the instructions in the platform’s documentation. This usually involves using the CLI to create a new application and specifying the programming language you are using.

Step 4: Configure your application

Configure your application by adding any required environment variables, setting up a database, and specifying the required dependencies. This can be done using the platform’s web interface or CLI.

Step 5: Deploy your application

Deploy your application to the cloud platform using the platform’s deployment process. This usually involves pushing your code to a Git repository and triggering a build process.

Step 6: Test your application

Once your application is deployed, test it to make sure it is working as expected. You can access your application using the URL provided by the cloud platform.

Step 7: Scale your application

If your application needs to handle a large amount of traffic, you can scale it up by adding more instances or resources. This can be done using the platform’s web interface or CLI.

That’s it! Deploying a web application on a cloud platform can be complex, but most platforms have detailed documentation and support to help you through the process.

Docker is an open-source platform that allows developers to create, deploy, and run applications in containers. Containers are lightweight, standalone environments that contain all the necessary dependencies, libraries, and configuration files to run an application.

Docker simplifies the process of creating and managing containers by providing a standardized format for packaging applications and their dependencies. This format is called a Docker image, and it contains everything needed to run the application, including the code, dependencies, and configuration files.

With Docker, developers can easily create, test, and deploy applications in a consistent and repeatable way, regardless of the underlying infrastructure. Docker provides a flexible and portable platform that can run on any operating system, cloud provider, or server.

In addition to simplifying application deployment, Docker also provides a number of benefits, such as improved resource utilization, faster development cycles, and better collaboration between teams. It is widely used in the software development industry and has become an essential tool for building and deploying modern applications.

To create a Docker image for a simple web app, you can follow these steps:

  1. Write your web app code and create a Dockerfile in the same directory as your code.

  2. In the Dockerfile, specify the base image you want to use. For example, if you’re building a Python web app, you could use the official Python image as your base image:

FROM python:3.9
  1. Install any dependencies your web app requires using the appropriate package manager. For example, if you’re using pip, you could add the following to your Dockerfile:

COPY requirements.txt /app/
RUN pip install --no-cache-dir -r /app/requirements.txt
  1. Copy your web app code into the container:

COPY . /app
  1. Set the working directory to your app directory:

WORKDIR /app
  1. Expose the port your web app listens on:

EXPOSE 8000
  1. Specify the command to start your web app. For example, if you’re using Django, you could add the following to your Dockerfile:

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
  1. Build the Docker image:

docker build -t my-web-app .
  1. Run the Docker container:

docker run -p 8000:8000 my-web-app

This is a basic example, and the exact steps will vary depending on your web app and its dependencies. However, this should give you an idea of how to create a Docker image for a simple web app.

Making http requests#

The requests library is a powerful Python library that allows you to send HTTP/1.1 requests using Python. It is easy to use and it can be installed using pip. Here are the steps to use requests in Python:

  1. Install the requests library: Open the terminal or command prompt and type the following command:

pip install requests
  1. Import the requests module: To use requests, you need to import it at the beginning of your Python script. Here is the code to import requests:

import requests
url = "https://google.com"
  1. Sending HTTP requests: The requests library provides several methods to send HTTP requests such as GET, POST, PUT, DELETE, etc. The most commonly used methods are GET and POST. For example, to send a GET request, you can use the requests.get() method:

response = requests.get("https://google.com")

where url is the URL of the website you want to request.

To send a POST request, you can use the requests.post() method:

  1. Handling the response: After sending the request, you will get a response from the server. The response object contains the response from the server. Here is an example of how to handle the response:

response = requests.get(url)
print(response.status_code)
print(response.content)
200
b'<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en"><head><meta content="Search the world\'s information, including webpages, images, videos and more. Google has many special features to help you find exactly what you\'re looking for." name="description"><meta content="noodp, " name="robots"><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="rS9vQYMvI8q4T_FcdrOtqQ">(function(){var _g={kEI:\'dZ_8ZqqMFKSzptQPnYy6uAE\',kEXPI:\'0,3700332,617,432,3,538661,2872,2891,562,17988,24478,30022,16105,18165,162091,342,23024,6700,106647,17666,2006,8155,23350,8703,13733,9779,44499,18158,6050,30697,3801,2412,33249,15816,1804,7734,18098,575,687,8175,11813,1632,13496,15783,27083,5212676,998,148,5991284,2840361,741,880,1,37,9,3,1,21,27,4,38,23936303,4043709,16673,43886,3,1603,3,2124363,23034775,2739,4636,16436,84045,22623,884,14280,8182,49429,19011,2660,2481,1,1,1,950,3319,155,1,1,1,2481,13504,7736,9139,744,1,3,3851,328,3217,4,1235,1769,6760,6132,7709,4858,5,820,37,7303,686,7796,3,1,3014,13256,5785,580,102,283,169,207,13653,55,2204,7,12,9273,4134,2382,1484,979,524,4,2766,1,1938,5556,273,1667,238,6165,688,1539,4176,797,4544,1240,2899,1,3967,1404,2815,1766,5348,2222,1330,1544,1,2885,1,1,2,3,3503,1,1604,8,2983,256,597,1802,934,380,521,1494,2,1015,1518,3276,257,849,854,869,1,1066,1,1,2,3,1451,41,243,1,1669,2,1,1,2,3,830,596,1236,1037,411,917,1081,486,246,2345,774,1159,199,531,1623,202,193,473,640,811,138,629,129,50,289,186,191,721,57,3,495,163,95,577,3,341,2708,624,143,989,387,8,46,1,6,72,298,517,952,168,56,49,114,1496,1204,4,342,132,28,4,1,6,89,610,702,1287,706,92,152,126,35,279,1015,1384,809,6,737,157,27,32,2202,8,1,27,210,459,743,484,9,165,49,96,64,700,544,8,1,1,4,1,4,678,3,2362,387,596,86,46,31,12,244,324,24,717,63,174,3,459,89,413,4,5,74,872,3,7,54,3,221,2,639,151,1,54,114,914,900,2,449,7,829,825,884,1184,417,21434818,4294,3,1423,3,14475,3,16997,18,149,69,2398,94,70,460,1110,362\',kBL:\'Pj_6\',kOPI:89978449};(function(){var a;((a=window.google)==null?0:a.stvsc)?google.kEI=_g.kEI:window.google=_g;}).call(this);})();(function(){google.sn=\'webhp\';google.kHL=\'en\';})();(function(){\nvar h=this||self;function l(){return window.google!==void 0&&window.google.kOPI!==void 0&&window.google.kOPI!==0?window.google.kOPI:null};var m,n=[];function p(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||m}function q(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b}function r(a){/^http:/i.test(a)&&window.location.protocol==="https:"&&(google.ml&&google.ml(Error("a"),!1,{src:a,glmm:1}),a="");return a}\nfunction t(a,b,c,d,k){var e="";b.search("&ei=")===-1&&(e="&ei="+p(d),b.search("&lei=")===-1&&(d=q(d))&&(e+="&lei="+d));d="";var g=b.search("&cshid=")===-1&&a!=="slh",f=[];f.push(["zx",Date.now().toString()]);h._cshid&&g&&f.push(["cshid",h._cshid]);c=c();c!=null&&f.push(["opi",c.toString()]);for(c=0;c<f.length;c++){if(c===0||c>0)d+="&";d+=f[c][0]+"="+f[c][1]}return"/"+(k||"gen_204")+"?atyp=i&ct="+String(a)+"&cad="+(b+e+d)};m=google.kEI;google.getEI=p;google.getLEI=q;google.ml=function(){return null};google.log=function(a,b,c,d,k,e){e=e===void 0?l:e;c||(c=t(a,b,e,d,k));if(c=r(c)){a=new Image;var g=n.length;n[g]=a;a.onerror=a.onload=a.onabort=function(){delete n[g]};a.src=c}};google.logUrl=function(a,b){b=b===void 0?l:b;return t("",a,b)};}).call(this);(function(){google.y={};google.sy=[];var d;(d=google).x||(d.x=function(a,b){if(a)var c=a.id;else{do c=Math.random();while(google.y[c])}google.y[c]=[a,b];return!1});var e;(e=google).sx||(e.sx=function(a){google.sy.push(a)});google.lm=[];var f;(f=google).plm||(f.plm=function(a){google.lm.push.apply(google.lm,a)});google.lq=[];var g;(g=google).load||(g.load=function(a,b,c){google.lq.push([[a],b,c])});var h;(h=google).loadAll||(h.loadAll=function(a,b){google.lq.push([a,b])});google.bx=!1;var k;(k=google).lx||(k.lx=function(){});var l=[],m;(m=google).fce||(m.fce=function(a,b,c,n){l.push([a,b,c,n])});google.qce=l;}).call(this);google.f={};(function(){\ndocument.documentElement.addEventListener("submit",function(b){var a;if(a=b.target){var c=a.getAttribute("data-submitfalse");a=c==="1"||c==="q"&&!a.elements.q.value?!0:!1}else a=!1;a&&(b.preventDefault(),b.stopPropagation())},!0);document.documentElement.addEventListener("click",function(b){var a;a:{for(a=b.target;a&&a!==document.documentElement;a=a.parentElement)if(a.tagName==="A"){a=a.getAttribute("data-nohref")==="1";break a}a=!1}a&&b.preventDefault()},!0);}).call(this);</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}\n</style><style>body,td,a,p,.h{font-family:arial,sans-serif}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#1967d2}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}body{background:#fff;color:#000}a{color:#681da8;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#1967d2}a:visited{color:#681da8}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}.lsbb{background:#f8f9fa;border:solid 1px;border-color:#dadce0 #70757a #70757a #dadce0;height:30px}.lsbb{display:block}#WqQANb a{display:inline-block;margin:0 12px}.lsb{background:url(/images/nav_logo229.png) 0 -261px repeat-x;color:#000;border:none;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#dadce0}.lst:focus{outline:none}</style><script nonce="rS9vQYMvI8q4T_FcdrOtqQ">(function(){window.google.erd={jsr:1,bv:2090,de:true,dpf:\'GVCNSPh-pH6lTGENK2sZWtwG7cTmNzhuySOvXJj752Q\'};\nvar g=this||self;var k,l=(k=g.mei)!=null?k:1,n,p=(n=g.sdo)!=null?n:!0,q=0,r,t=google.erd,v=t.jsr;google.ml=function(a,b,d,m,e){e=e===void 0?2:e;b&&(r=a&&a.message);d===void 0&&(d={});d.cad="ple_"+google.ple+".aple_"+google.aple;if(google.dl)return google.dl(a,e,d,!0),null;b=d;if(v<0){window.console&&console.error(a,b);if(v===-2)throw a;b=!1}else b=!a||!a.message||a.message==="Error loading script"||q>=l&&!m?!1:!0;if(!b)return null;q++;d=d||{};b=encodeURIComponent;var c="/gen_204?atyp=i&ei="+b(google.kEI);google.kEXPI&&(c+="&jexpid="+b(google.kEXPI));c+="&srcpg="+b(google.sn)+"&jsr="+b(t.jsr)+\n"&bver="+b(t.bv);t.dpf&&(c+="&dpf="+b(t.dpf));var f=a.lineNumber;f!==void 0&&(c+="&line="+f);var h=a.fileName;h&&(h.indexOf("-extension:/")>0&&(e=3),c+="&script="+b(h),f&&h===window.location.href&&(f=document.documentElement.outerHTML.split("\\n")[f],c+="&cad="+b(f?f.substring(0,300):"No script found.")));google.ple&&google.ple===1&&(e=2);c+="&jsel="+e;for(var u in d)c+="&",c+=b(u),c+="=",c+=b(d[u]);c=c+"&emsg="+b(a.name+": "+a.message);c=c+"&jsst="+b(a.stack||"N/A");c.length>=12288&&(c=c.substr(0,12288));a=c;m||google.log(0,"",a);return a};window.onerror=function(a,b,d,m,e){r!==a&&(a=e instanceof Error?e:Error(a),d===void 0||"lineNumber"in a||(a.lineNumber=d),b===void 0||"fileName"in a||(a.fileName=b),google.ml(a,!1,void 0,!1,a.name==="SyntaxError"||a.message.substring(0,11)==="SyntaxError"||a.message.indexOf("Script error")!==-1?3:0));r=null;p&&q>=l&&(window.onerror=null)};})();</script></head><body bgcolor="#fff"><script nonce="rS9vQYMvI8q4T_FcdrOtqQ">(function(){var src=\'/images/nav_logo229.png\';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;}\nif (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}\n}\n})();</script><div id="mngb"><div id=gbar><nobr><b class=gb1>Search</b> <a class=gb1 href="https://www.google.com/imghp?hl=en&tab=wi">Images</a> <a class=gb1 href="https://maps.google.com/maps?hl=en&tab=wl">Maps</a> <a class=gb1 href="https://play.google.com/?hl=en&tab=w8">Play</a> <a class=gb1 href="https://www.youtube.com/?tab=w1">YouTube</a> <a class=gb1 href="https://news.google.com/?tab=wn">News</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">Drive</a> <a class=gb1 style="text-decoration:none" href="https://www.google.com/intl/en/about/products?tab=wh"><u>More</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.com/history/optout?hl=en" class=gb4>Web History</a> | <a  href="/preferences?hl=en" class=gb4>Settings</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/&ec=GAZAAQ" class=gb4>Sign in</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div></div><center><br clear="all" id="lgpd"><div id="XjhHGf"><img alt="Google" height="92" src="/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png" style="padding:28px 0 14px" width="272" id="hplogo"><br><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowrap=""><input name="ie" value="ISO-8859-1" type="hidden"><input value="en" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><input name="biw" type="hidden"><input name="bih" type="hidden"><div class="ds" style="height:32px;margin:4px 0"><input class="lst" style="margin:0;padding:5px 8px 0 6px;vertical-align:top;color:#000" autocomplete="off" value="" title="Google Search" maxlength="2048" name="q" size="57"></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Google Search" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" id="tsuid_1" value="I\'m Feeling Lucky" name="btnI" type="submit"><script nonce="rS9vQYMvI8q4T_FcdrOtqQ">(function(){var id=\'tsuid_1\';document.getElementById(id).onclick = function(){if (this.form.q.value){this.checked = 1;if (this.form.iflsig)this.form.iflsig.disabled = false;}\nelse top.location=\'/doodles/\';};})();</script><input value="AL9hbdgAAAAAZvythX2E_gA1Gdwy-i-pau92B656hxG6" name="iflsig" type="hidden"></span></span></td><td class="fl sblc" align="left" nowrap="" width="25%"><a href="/advanced_search?hl=en&amp;authuser=0">Advanced search</a></td></tr></table><input id="gbv" name="gbv" type="hidden" value="1"><script nonce="rS9vQYMvI8q4T_FcdrOtqQ">(function(){var a,b="1";if(document&&document.getElementById)if(typeof XMLHttpRequest!="undefined")b="2";else if(typeof ActiveXObject!="undefined"){var c,d,e=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];for(c=0;d=e[c++];)try{new ActiveXObject(d),b="2"}catch(h){}}a=b;if(a=="2"&&location.search.indexOf("&gbv=2")==-1){var f=google.gbvu,g=document.getElementById("gbv");g&&(g.value=a);f&&window.setTimeout(function(){location.href=f},0)};}).call(this);</script></form><div style="font-size:83%;min-height:3.5em"><br></div><span id="footer"><div style="font-size:10pt"><div style="margin:19px auto;text-align:center" id="WqQANb"><a href="/intl/en/ads/">Advertising</a><a href="/services/">Business Solutions</a><a href="/intl/en/about.html">About Google</a></div></div><p style="font-size:8pt;color:#70757a">&copy; 2024 - <a href="/intl/en/policies/privacy/">Privacy</a> - <a href="/intl/en/policies/terms/">Terms</a></p></span></center><script nonce="rS9vQYMvI8q4T_FcdrOtqQ">(function(){window.google.cdo={height:757,width:1440};(function(){var a=window.innerWidth,b=window.innerHeight;if(!a||!b){var c=window.document,d=c.compatMode=="CSS1Compat"?c.documentElement:c.body;a=d.clientWidth;b=d.clientHeight}\nif(a&&b&&(a!=google.cdo.width||b!=google.cdo.height)){var e=google,f=e.log,g="/client_204?&atyp=i&biw="+a+"&bih="+b+"&ei="+google.kEI,h="",k=[],l=window.google!==void 0&&window.google.kOPI!==void 0&&window.google.kOPI!==0?window.google.kOPI:null;l!=null&&k.push(["opi",l.toString()]);for(var m=0;m<k.length;m++){if(m===0||m>0)h+="&";h+=k[m][0]+"="+k[m][1]}f.call(e,"","",g+h)};}).call(this);})();</script>  <script nonce="rS9vQYMvI8q4T_FcdrOtqQ">(function(){google.xjs={basecomb:\'/xjs/_/js/k\\x3dxjs.hp.en.r1LW2qHo_Gs.es5.O/ck\\x3dxjs.hp.kexfO286WYk.L.X.O/am\\x3dAQAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAQAgAAAAAAAAAAGAAADkAAAAEAAgIAAAAADgAAAAAAAAQgAgAQAFAmAAADsR3BAACYBEAAF4/d\\x3d1/ed\\x3d1/dg\\x3d0/ujg\\x3d1/rs\\x3dACT90oFjadLQKbPXJJfuP0bIm6LsclGxzg\',basecss:\'/xjs/_/ss/k\\x3dxjs.hp.kexfO286WYk.L.X.O/am\\x3dAQAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAEAAAAEAAgIAAAAAAAAAAAAAAAQgAgAQAFAmA/rs\\x3dACT90oG_vyprMj2LGjy1N_BBuTkyRqnZdA\',basejs:\'/xjs/_/js/k\\x3dxjs.hp.en.r1LW2qHo_Gs.es5.O/am\\x3dAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAGAAADgAAAAAAAAAAAAAADgAAAAAAAAAAAAAAAAAAAAADsR3BAACYBEAAF4/dg\\x3d0/rs\\x3dACT90oEG1SjRrg8F18v1w8efFdS8rgL8ZA\',excm:[]};})();</script>  <link href="/xjs/_/ss/k=xjs.hp.kexfO286WYk.L.X.O/am=AQAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAEAAAAEAAgIAAAAAAAAAAAAAAAQgAgAQAFAmA/d=1/ed=1/rs=ACT90oG_vyprMj2LGjy1N_BBuTkyRqnZdA/m=sb_he,d" rel="stylesheet" nonce="rS9vQYMvI8q4T_FcdrOtqQ">      <script nonce="rS9vQYMvI8q4T_FcdrOtqQ">(function(){var u=\'/xjs/_/js/k\\x3dxjs.hp.en.r1LW2qHo_Gs.es5.O/am\\x3dAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAGAAADgAAAAAAAAAAAAAADgAAAAAAAAAAAAAAAAAAAAADsR3BAACYBEAAF4/d\\x3d1/ed\\x3d1/dg\\x3d3/rs\\x3dACT90oEG1SjRrg8F18v1w8efFdS8rgL8ZA/m\\x3dsb_he,d\';var st=1;var amd=1000;var mmd=0;var pod=true;var f=typeof Object.defineProperties=="function"?Object.defineProperty:function(a,b,c){if(a==Array.prototype||a==Object.prototype)return a;a[b]=c.value;return a},g=function(a){a=["object"==typeof globalThis&&globalThis,a,"object"==typeof window&&window,"object"==typeof self&&self,"object"==typeof global&&global];for(var b=0;b<a.length;++b){var c=a[b];if(c&&c.Math==Math)return c}throw Error("a");},h=g(this),k=function(a,b){if(b)a:{var c=h;a=a.split(".");for(var d=0;d<a.length-1;d++){var e=a[d];if(!(e in\nc))break a;c=c[e]}a=a[a.length-1];d=c[a];b=b(d);b!=d&&b!=null&&f(c,a,{configurable:!0,writable:!0,value:b})}};k("globalThis",function(a){return a||h});\nvar l=this||self;function m(){var a,b,c;return(a=(b=window.google)==null?void 0:(c=b.ia)==null?void 0:c.r.B2Jtyd)&&a.m===1&&a.cbfd!=null&&a.cbvi!=null?a:void 0};\nvar n=globalThis.trustedTypes,p;function q(){var a=null;if(!n)return a;try{var b=function(c){return c};a=n.createPolicy("goog#html",{createHTML:b,createScript:b,createScriptURL:b})}catch(c){}return a};var r=function(a){this.g=a};r.prototype.toString=function(){return this.g+""};function t(a){if(a instanceof r)return a.g;throw Error("b");};var w=/^\\s*(?!javascript:)(?:[\\w+.-]+:|[^:/?#]*(?:[/?#]|$))/i;var x="alternate author bookmark canonical cite help icon license modulepreload next prefetch dns-prefetch prerender preconnect preload prev search subresource".split(" ");function y(a,b){a.src=t(b);var c,d;(c=(b=(d=(c=(a.ownerDocument&&a.ownerDocument.defaultView||window).document).querySelector)==null?void 0:d.call(c,"script[nonce]"))?b.nonce||b.getAttribute("nonce")||"":"")&&a.setAttribute("nonce",c)};var z=function(a){var b=document;a=String(a);b.contentType==="application/xhtml+xml"&&(a=a.toLowerCase());return b.createElement(a)};function A(a){a=a===null?"null":a===void 0?"undefined":a;p===void 0&&(p=q());var b=p;return new r(b?b.createScriptURL(a):a)};google.ps===void 0&&(google.ps=[]);function B(){var a=u,b=function(){};google.lx=google.stvsc?b:function(){C(a);google.lx=b};google.bx||google.lx()}function D(a,b){b&&y(a,A(b));var c=a.onload;a.onload=function(d){c&&c(d);google.ps=google.ps.filter(function(e){return a!==e})};google.ps.push(a);document.body.appendChild(a)}google.as=D;function C(a){google.timers&&google.timers.load&&google.tick&&google.tick("load","xjsls");var b=z("SCRIPT");b.onerror=function(){google.ple=1};b.onload=function(){google.ple=0};google.xjsus=void 0;D(b,a);google.aple=-1;google.dp=!0}\nfunction E(){var a=[u];if(!google.dp){for(var b=0;b<a.length;b++){var c=z("LINK"),d=c,e=A(a[b]);if(e instanceof r)d.href=t(e).toString(),d.rel="preload";else{if(x.indexOf("preload")===-1)throw Error("c`preload");e=w.test(e)?e:void 0;e!==void 0&&(d.href=e,d.rel="preload")}c.setAttribute("as","script");document.body.appendChild(c)}google.dp=!0}};function F(a){var b=a.getAttribute("jscontroller");return(b==="UBXHI"||b==="R3fhkb"||b==="TSZEqd")&&a.hasAttribute("data-src")}function G(){for(var a=document.getElementsByTagName("img"),b=0,c=a.length;b<c;b++){var d=a[b];if(d.hasAttribute("data-lzy_")&&Number(d.getAttribute("data-atf"))&1&&!F(d))return!0}return!1}for(var H=document.getElementsByTagName("img"),I=0,J=H.length;I<J;++I){var K=H[I];Number(K.getAttribute("data-atf"))&1&&F(K)&&(K.src=K.getAttribute("data-src"))};var L,M,N,O,P,Q;function R(){google.xjsu=u;l._F_jsUrl=u;P=function(){B()};L=!1;M=(st===1||st===3)&&!!google.caft&&!G();N=m();O=(st===2||st===3)&&(!!google.rairicb||!!N)&&!G();Q=pod}function S(){L||M||O||(P(),L=!0)}\nsetTimeout(function(){google&&google.tick&&google.timers&&google.timers.load&&google.tick("load","xjspls");R();if(M||O){if(M){var a=function(){M=!1;S()};google.caft(a);window.setTimeout(a,amd)}O&&(a=function(){O=!1;S()},N?N.cbvi.push(a):(0,google.rairicb)(a),window.setTimeout(a,mmd));Q&&(L||E())}else P()},0);})();window._ = window._ || {};window._DumpException = _._DumpException = function(e){throw e;};window._s = window._s || {};_s._DumpException = _._DumpException;window._qs = window._qs || {};_qs._DumpException = _._DumpException;(function(){var t=[1,16,0,0,0,0,8683520,0,332608,606077328,515,33685509,4194304,224,0,139268,407978304,272105474,536889212,284736,24064];window._F_toggles = window._xjs_toggles = t;})();window._F_installCss = window._F_installCss || function(css){};(function(){google.jl={bfl:0,dw:false,ine:false,ubm:false,uwp:true,vs:false};})();(function(){var pmc=\'{\\x22d\\x22:{},\\x22sb_he\\x22:{\\x22agen\\x22:false,\\x22cgen\\x22:false,\\x22client\\x22:\\x22heirloom-hp\\x22,\\x22dh\\x22:true,\\x22ds\\x22:\\x22\\x22,\\x22fl\\x22:true,\\x22host\\x22:\\x22google.com\\x22,\\x22jsonp\\x22:true,\\x22msgs\\x22:{\\x22cibl\\x22:\\x22Clear Search\\x22,\\x22dym\\x22:\\x22Did you mean:\\x22,\\x22lcky\\x22:\\x22I\\\\u0026#39;m Feeling Lucky\\x22,\\x22lml\\x22:\\x22Learn more\\x22,\\x22psrc\\x22:\\x22This search was removed from your \\\\u003Ca href\\x3d\\\\\\x22/history\\\\\\x22\\\\u003EWeb History\\\\u003C/a\\\\u003E\\x22,\\x22psrl\\x22:\\x22Remove\\x22,\\x22sbit\\x22:\\x22Search by image\\x22,\\x22srch\\x22:\\x22Google Search\\x22},\\x22ovr\\x22:{},\\x22pq\\x22:\\x22\\x22,\\x22rfs\\x22:[],\\x22stok\\x22:\\x22bfHp37wdsuXQzIWWKVLUFpqo9r0\\x22}}\';google.pmc=JSON.parse(pmc);})();(function(){var b=function(a){var c=0;return function(){return c<a.length?{done:!1,value:a[c++]}:{done:!0}}};\nvar e=this||self;var g,h;a:{for(var k=["CLOSURE_FLAGS"],l=e,n=0;n<k.length;n++)if(l=l[k[n]],l==null){h=null;break a}h=l}var p=h&&h[610401301];g=p!=null?p:!1;var q,r=e.navigator;q=r?r.userAgentData||null:null;function t(a){return g?q?q.brands.some(function(c){return(c=c.brand)&&c.indexOf(a)!=-1}):!1:!1}function u(a){var c;a:{if(c=e.navigator)if(c=c.userAgent)break a;c=""}return c.indexOf(a)!=-1};function v(){return g?!!q&&q.brands.length>0:!1}function w(){return u("Safari")&&!(x()||(v()?0:u("Coast"))||(v()?0:u("Opera"))||(v()?0:u("Edge"))||(v()?t("Microsoft Edge"):u("Edg/"))||(v()?t("Opera"):u("OPR"))||u("Firefox")||u("FxiOS")||u("Silk")||u("Android"))}function x(){return v()?t("Chromium"):(u("Chrome")||u("CriOS"))&&!(v()?0:u("Edge"))||u("Silk")}function y(){return u("Android")&&!(x()||u("Firefox")||u("FxiOS")||(v()?0:u("Opera"))||u("Silk"))};var z=v()?!1:u("Trident")||u("MSIE");y();x();w();var A=!z&&!w(),D=function(a){if(/-[a-z]/.test("ved"))return null;if(A&&a.dataset){if(y()&&!("ved"in a.dataset))return null;a=a.dataset.ved;return a===void 0?null:a}return a.getAttribute("data-"+"ved".replace(/([A-Z])/g,"-$1").toLowerCase())};var E=[],F=null;function G(a){a=a.target;var c=performance.now(),f=[],H=f.concat,d=E;if(!(d instanceof Array)){var m=typeof Symbol!="undefined"&&Symbol.iterator&&d[Symbol.iterator];if(m)d=m.call(d);else if(typeof d.length=="number")d={next:b(d)};else throw Error("b`"+String(d));for(var B=[];!(m=d.next()).done;)B.push(m.value);d=B}E=H.call(f,d,[c]);if(a&&a instanceof HTMLElement)if(a===F){if(c=E.length>=4)c=(E[E.length-1]-E[E.length-4])/1E3<5;if(c){c=google.getEI(a);a.hasAttribute("data-ved")?f=a?D(a)||"":"":f=(f=\na.closest("[data-ved]"))?D(f)||"":"";f=f||"";if(a.hasAttribute("jsname"))a=a.getAttribute("jsname");else{var C;a=(C=a.closest("[jsname]"))==null?void 0:C.getAttribute("jsname")}google.log("rcm","&ei="+c+"&tgtved="+f+"&jsname="+(a||""))}}else F=a,E=[c]}window.document.addEventListener("DOMContentLoaded",function(){document.body.addEventListener("click",G)});}).call(this);</script></body></html>'

where status_code is the HTTP status code returned by the server, and content is the content of the response.

That’s it! These are the basic steps to use requests in Python. With requests, you can easily send HTTP requests, handle responses, and work with APIs.