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

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
from rqsession import RequestSession
session = RequestSession()
session.initialize_session()
session.print_log = True
print("Current session headers:", session.headers)
session.get("https://google.com")

from rqsession import RequestSession
session = RequestSession()
session.initialize_session(random_init=True)
session.set_proxy(use_proxy=True, random_proxy=True)
response = session.get("https://example.com")
session.save_session(_id="my_session")
loaded_session = RequestSession.load_session("tmp/http_session/my_session.json")
Advanced Usage
Proxy Configuration
session = RequestSession(
config={
"host": "127.0.0.1",
"port": "8080",
"enabled": True,
"random_proxy": True,
"proxy_file": "path/to/proxies.txt"
}
)
def get_my_proxy():
return "http://user:pass@proxy.example.com:8080"
session = RequestSession(proxy_method=get_my_proxy)
Session Management
session.save_session(_id="my_saved_session")
loaded_session = RequestSession.load_session("tmp/http_session/my_saved_session.json")
domain_cookies = session.get_cookies_for_domain("example.com")
cookie_string = session.get_cookies_string(domain="example.com")
Request History and Logging
session.set_print_log(True)
session.get("https://example.com/page1")
session.post("https://example.com/api", json={"key": "value"})
recent_requests = session.get_request_history(limit=5)
successful_requests = session.get_request_history(
filter_func=lambda r: r["status_code"] == 200
)
session.export_request_chain(filepath="request_history.json")
session.clear_history()
Cookie Management
session.set_cookies({
"session_id": "abc123",
"user_preferences": "dark_mode"
})
session.set_cookies([
{
"name": "session_id",
"value": "abc123",
"domain": "example.com",
"path": "/",
"secure": True,
"httponly": True
}
])
session.set_cookies("name1=value1; name2=value2")
Configuration
The RequestSession can be configured with the following options:
| 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.