HTTP Requests and Responses
When exchanging data between a web browser (client) and a web server on the internet, a protocol called HTTP (HyperText Transfer Protocol) is used.
When the web browser sends a Request
to the web server, the web server returns a Response
for that request.
HTTP Request
An HTTP Request
refers to a message sent from the web browser to the web server.
For instance, when opening a specific web page or sending user-input data to the web server, the web browser sends an HTTP request to that web server.
The most basic types of requests include:
-
GET: Requests data from the server. For example, it is used when loading a web page or image.
-
POST: Sends data to the server to process a resource based on the submitted data. For example, it is used when submitting a web form.
A request can include an address (URL), a request method (e.g., GET, POST), headers (e.g., metadata about the request and cookies), and a request body (data sent with a POST request).
# Sending an HTTP request using the requests library import requests url = 'http://example.com' # URL of the web page to request response = requests.get(url) # Sending a GET request print(response.text) # Output the HTML content of the web page
HTTP Response
An HTTP Response
is a message sent by the web server to the client in response to a request.
This response includes a status code indicating the success/failure of the request, server information, and data returned from the server.
Examples of status codes are:
-
200 OK: Indicates that the request was successfully processed.
-
404 Not Found: Indicates that the requested resource could not be found.
-
500 Internal Server Error: Indicates that an internal server error occurred.
A response can also include headers and a body.
The headers provide metadata about the response (e.g., content type), and the body contains the actual returned data (HTML document, image, etc.).
# Receiving an HTTP response using the requests library import requests url = 'http://example.com' response = requests.get(url) print(response.status_code) # Output the status code print(response.headers) # Output the response headers print(response.text) # Output the response body
The Process of Loading a Web Page
-
Request: When a user enters a URL in the address bar of a web browser and presses enter, the browser sends an HTTP request to the web server for that URL.
-
Processing: The web server processes the request and returns the requested data and web page as an HTTP response to the client.
-
Response: The browser parses the response result and renders the contents of the web page on the screen.
Practice
Click the Run Code
button on the right side of the screen to see the results of crawling or modify the code!
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result