Hone logo
Hone
Problems

Efficient Web Requests with Python's Requests Session

The requests library is a powerful tool for making HTTP requests in Python. However, repeatedly creating requests objects for each request can be inefficient, especially when interacting with the same server multiple times. This challenge focuses on utilizing requests.Session to improve performance and manage persistent connections, cookies, and authentication.

Problem Description

You are tasked with creating a Python script that uses a requests.Session object to make multiple requests to a specified URL. The script should perform the following actions:

  1. Create a Session: Initialize a requests.Session object.
  2. Make Multiple Requests: Make three GET requests to the same base URL (provided as input). Each request should have a different query parameter appended to the URL. The query parameters should be "param1", "param2", and "param3".
  3. Print Responses: For each request, print the status code and the first 100 characters of the response content. If the response content is less than 100 characters, print the entire content.
  4. Close the Session: Properly close the session after all requests are completed.

The use of a requests.Session is crucial for demonstrating the benefits of persistent connections and efficient resource management. Failure to use a session will result in a significant penalty.

Examples

Example 1:

Input: "https://httpbin.org/get"
Output:
Status Code: 200
Content: {
  "args": {"param1": ""},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.31.0",
    "X-Amzn-Trace-Id": "Root=1-654a1234-4a1234567890abcdeffedcba"
  },
  "json": null,
  "origin": "...",
  "url": "https://httpbin.org/get?param1="
}
Status Code: 200
Content: {
  "args": {"param2": ""},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.31.0",
    "X-Amzn-Trace-Id": "Root=1-654a1234-4a1234567890abcdeffedcba"
  },
  "json": null,
  "origin": "...",
  "url": "https://httpbin.org/get?param2="
}
Status Code: 200
Content: {
  "args": {"param3": ""},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.31.0",
    "X-Amzn-Trace-Id": "Root=1-654a1234-4a1234567890abcdeffedcba"
  },
  "json": null,
  "origin": "...",
  "url": "https://httpbin.org/get?param3="
}

Explanation: The script makes three GET requests to httpbin.org/get with different query parameters. The output shows the status code and the first 100 characters (or the entire content if shorter) of each response.

Example 2:

Input: "https://www.example.com"
Output:
Status Code: 200
Content: <!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Example domain" />
...
Status Code: 200
Content: <!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Example domain" />
...
Status Code: 200
Content: <!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Example domain" />
...

Explanation: The script makes three GET requests to www.example.com with different query parameters. The output shows the status code and the first 100 characters (or the entire content if shorter) of each response.

Constraints

  • The input URL must be a valid string.
  • The script must use the requests library.
  • The script must utilize a requests.Session object.
  • The script must handle cases where the response content is less than 100 characters.
  • The script must close the session after making all requests.
  • The URL provided should be accessible. Using an inaccessible URL will result in an error.

Notes

  • Consider using a try...except block to handle potential network errors or invalid URLs.
  • The httpbin.org service is useful for testing HTTP requests.
  • Remember to install the requests library: pip install requests
  • The order of the requests is not important.
  • Focus on demonstrating the correct usage of requests.Session for efficient request handling.
Loading editor...
python