Fake HTTP Header
fake-http-header is a Python package that generates random request fields for an HTTP header. The generated header fields emulate real internet browser behavior by mapping browsers to default values and user actions. This package is useful for scenarios such as web crawling or testing web applications.
Warnings
- gotcha The `FakeHttpHeader` object itself is not a dictionary. To use the generated headers with HTTP client libraries (e.g., `requests`, `httpx`), you must call the `.as_header_dict()` method to get a dictionary representation.
- gotcha The library has not been updated since December 2021 (version 0.3.5). While its core functionality remains, the realism of generated User-Agents and other browser-specific headers might degrade over time as browsers and web standards evolve.
Install
-
pip install fake-http-header
Imports
- FakeHttpHeader
from fake_http_header import FakeHttpHeader
Quickstart
from fake_http_header import FakeHttpHeader
import requests
import os
# Generate a random HTTP header
fake_header_generator = FakeHttpHeader()
# Print the generated header object
print("Generated FakeHttpHeader object:", fake_header_generator)
# To use with libraries like 'requests', convert it to a dictionary
headers_dict = fake_header_generator.as_header_dict()
print("Header dictionary for requests:", headers_dict)
# Example of using with requests (replace with a real URL if testing live)
# This is just an example; it won't actually make a request without a URL
# For testing, you might use a service like httpbin.org
# try:
# response = requests.get('https://httpbin.org/headers', headers=headers_dict)
# response.raise_for_status() # Raise an exception for HTTP errors
# print("Response from httpbin:", response.json())
# except requests.exceptions.RequestException as e:
# print(f"An error occurred: {e}")