Skip to main content

Python requests session wrapper with advanced features for proxy management, session persistence, cookies auto update and request logging.

Project description

RequestSession

A powerful Python requests session wrapper with advanced features for proxy management, session persistence, and request logging.

PyPI version Python versions License

Features

  • 🌐 Proxy Management: Easy configuration of proxies with support for random rotation or fixed proxy per session
  • 💾 Session Persistence: Save and load sessions with cookies and headers, eliminating repetitive setup
  • 📝 Comprehensive Logging: Detailed request and response tracking with intelligent formatting and visualization
  • 🍪 Advanced Cookie Handling: Automatic domain-based cookie management and updates across different domains
  • 🔄 Request History: Track all requests with detailed metadata and exportable request chains for analysis
  • 🔧 Auto Headers: Automatic configuration of common headers like Host, Referer, and Origin, preventing resource access issues across different domains
  • 🚀 Simplified Workflow: Eliminates the hassle of manually building request signatures and managing session state

These features make RequestSession an ideal tool for web scraping, API testing, and automated browsing tasks where consistency and detailed tracking are essential.

Installation

pip install rqsession

Quick Start

  • simple 1
from rqsession import RequestSession

# Create a new session
session = RequestSession()
# Initialize with random base headers
session.initialize_session()
# print log
session.print_log = True

print("Current session headers:", session.headers)
session.get("https://google.com")

example 1

from rqsession import RequestSession

# Create a new session
session = RequestSession()

# Initialize with random headers
session.initialize_session(random_init=True)

# Enable proxy
session.set_proxy(use_proxy=True, random_proxy=True)

# Make a request
response = session.get("https://example.com")

# Save the session for later use
session.save_session(_id="my_session")

# Load a saved session
loaded_session = RequestSession.load_session("tmp/http_session/my_session.json")

Advanced Usage

Proxy Configuration

# Configure a session with custom proxy settings
session = RequestSession(
    config={
        "host": "127.0.0.1",
        "port": "8080",
        "enabled": True,
        "random_proxy": True,
        "proxy_file": "path/to/proxies.txt"
    }
)

# Use a custom proxy method
def get_my_proxy():
    return "http://user:pass@proxy.example.com:8080"

session = RequestSession(proxy_method=get_my_proxy)

Session Management

# Save the current session
session.save_session(_id="my_saved_session")

# Load a previously saved session
loaded_session = RequestSession.load_session("tmp/http_session/my_saved_session.json")

# Get all cookies for a specific domain
domain_cookies = session.get_cookies_for_domain("example.com")

# Export cookies as a string for use in other tools
cookie_string = session.get_cookies_string(domain="example.com")

Request History and Logging

# Enable detailed logging
session.set_print_log(True)

# Make some requests
session.get("https://example.com/page1")
session.post("https://example.com/api", json={"key": "value"})

# Get the last 5 requests
recent_requests = session.get_request_history(limit=5)

# Filter requests by status code
successful_requests = session.get_request_history(
    filter_func=lambda r: r["status_code"] == 200
)

# Export request history to a file
session.export_request_chain(filepath="request_history.json")

# Clear request history
session.clear_history()

Cookie Management

# Set cookies from a dictionary
session.set_cookies({
    "session_id": "abc123",
    "user_preferences": "dark_mode"
})

# Set cookies with full details
session.set_cookies([
    {
        "name": "session_id",
        "value": "abc123",
        "domain": "example.com",
        "path": "/",
        "secure": True,
        "httponly": True
    }
])

# Set cookies from a string
session.set_cookies("name1=value1; name2=value2")

Configuration

The RequestSession can be configured with the following options:

Option Description Default
host Proxy host From config.ini
port Proxy port From config.ini
enabled Enable proxy Based on config.ini
random_proxy Rotate proxies randomly False
print_log Enable detailed logging Based on config.ini
proxy_file File with proxy list "static/proxies.txt"
max_history_size Maximum number of requests to keep in history 100
auto_headers Automatically set common headers False
user_agents_file File with user agents "static/useragents.txt"
languages_file File with Accept-Language values "static/language.txt"
work_path Path for saving sessions and logs "tmp/http_session"

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the Apache License - see the LICENSE file for details.

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

rqsession-0.2.0.tar.gz (45.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rqsession-0.2.0-py3-none-any.whl (47.9 kB view details)

Uploaded Python 3

File details

Details for the file rqsession-0.2.0.tar.gz.

File metadata

  • Download URL: rqsession-0.2.0.tar.gz
  • Upload date:
  • Size: 45.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for rqsession-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a6fe7f075c6bbc9080f700674dc5d6ae6e104784455c249e9f9ad1171a4d9f28
MD5 a6c1d811e2a994a49893526dfd3f8410
BLAKE2b-256 3d0a310340dae3768b88f8b87ec5a1a5e318b0191290484f5ad1af64e5e3c133

See more details on using hashes here.

File details

Details for the file rqsession-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: rqsession-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 47.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for rqsession-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 724eed4cb00ab88fcb1e00b41e9026eb26f83a93dd963872c57f3ac52c5b5ab4
MD5 7b2cd71018a346bbb2a0d2f8961dcd94
BLAKE2b-256 18f0fc2e914da08135c207e04616c946ef79520cb1e0b0e10f4dda6215f99fb6

See more details on using hashes here.

Supported by

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