Skip to main content

Adds support for the Refresh header in requests. Check out the README for more info.

Project description

Requests Header Refresh

Description

This Python module provides a function to handle HTTP refresh headers in the requests library. It parses the refresh header, extracts or infers the refresh time and URL, and makes a subsequent request to the refresh URL if the refresh time is in a valid range. It also supports a maximum refresh to ignore the refresh if the refresh time is greater than a specified number of seconds.

Installation

You can use this module by importing it in your Python script. Make sure to have the requests-header-refresh library installed in your environment. If not, you can install it using pip:

pip install requests-header-refresh

You will also need the requests library to use this package.

Usage

First, import the necessary functions and classes:

import requests
from requests_header_refresh import create_hook

Then, create a session and a refresh handler with a timeout of 5 seconds:

session = requests.Session()
refresh_hook = create_hook(max_refresh=5)

Add the handler as a response hook:

session.hooks = {'response': [refresh_hook]}

Finally, make a request:

response = session.get(url)

In this example, refresh_hook will ignore the refresh if the refresh time is greater than 5 seconds. Replace url with the actual URL you want to make a request to. Also, adjust the max_refresh based on your needs.

The hook could also be used for a single request instead of entire session:

response = requests.get(url, hooks={'response': [refresh_hook]})

Contributing

Contributions are welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

requests_header_refresh-1.0.5.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

requests_header_refresh-1.0.5-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page