HTTP library that enables the use of OS certificate stores when working behind MITM firewalls
Project description
HTTP helper library for enterprise networks
Overview
sema4ai_http
-library provides HTTPS request handling that works inside enterprise networks that use MITM firewalls/proxies for outbound traffic.
The Problem:
- Modern firewalls need to track outbound traffic to detect malware
- To track the traffic, the SSL/TLS needs to be terminated on the firewall/proxy
- This means a separate certificate is needed for the internal network HTTPS to function
- These certificates are typically distributed using the OS-specific certificate stores
- Libraries like
requests
,urllib3
,aiohttp
,.. do not yet native support the certificate store
👉 In enterprise networks, HTTPS requests without the correct SSL context set will fail, and it is a hassle to get it right.
The solution
Every outbound HTTPS request needs the correct SSL context, but 95% of HTTPS code is just downloading files and simple GET / POST calls, so we provide a helper library.
We use truststore -library to access the certificate stores and urllib3
to avoid extra dependencies.
The key features of the library are:
- SSL context creation that uses OS certificate store and provided optional SSL legacy renegotiation support. - To use the SSL context with other libraries, please refer to the user guide from truststore
- HTTPS request methods (
GET
,POST
,PUT
,PATCH
,DELETE
) usingurllib3
. - Resumable file downloads with retry logic and error handling.
- Support for making downloaded files executable.
Usage Example
from pathlib import Path
from sema4ai_http import download_with_resume
url = "https://example.com/file.zip"
target = Path("/path/to/save/file.zip")
result = download_with_resume(url, target)
print(f"Download status: {result.status}")
print(f"File saved to: {result.path}")
Documentation
Functions:
1. HTTPS Request Functions
These functions handle different HTTPS request methods and return the response from urllib3
:
get(url, **kwargs)
: Sends a GET request.post(url, **kwargs)
: Sends a POST request.put(url, **kwargs)
: Sends a PUT request.patch(url, **kwargs)
: Sends a PATCH request.delete(url, **kwargs)
: Sends a DELETE request.
2. Build SSL Context
build_ssl_context(protocol: int = None, *, enable_legacy_server_connect: bool = False) -> ssl.SSLContext
**
This function creates an SSL context for use with urllib3
requests that use the truststore
library. It also supports enabling SSL legacy renegotiation connections.
Parameters:
protocol
: The SSL protocol to be used.enable_legacy_server_connect
: Enables support for legacy servers.
Returns: An SSL context with the appropriate configurations.
3. File download with resume support
download_with_resume(url: str, target: str | Path, **kwargs) -> DownloadResult
**
Downloads a file from a URL with support for resuming interrupted downloads. This function can also retry downloads multiple times in case of failure and ensures the file is downloaded completely before marking it as done.
Parameters:
url
: The URL of the file to download.target
: The target path where the file should be saved.headers
: Optional headers for the request.make_executable
: Whether to make the file executable.chunk_size
: The size of the data chunks to be downloaded.poll_manager
: Theurllib3.PoolManager
instance to use.max_retries
: Maximum number of retries for the download.timeout
: Timeout for the request.wait_interval
: Time to wait between retries.overwrite_existing
: Whether to overwrite an existing file.resume_from_existing_part_file
: Whether to resume the download from an existing partial file. Defaults to True.
Returns: A DownloadResult
object containing the download status and file path.
4. Partial file exists
partial_file_exists(target: str | Path) -> bool
**
A helper function to check if a partial download file exists for a given target path.
Parameters:
target
: The file path to check for an existing partial file.
Returns: A boolean indicating if a partial file exists.
Classes:
File download result class
DownloadResult
A NamedTuple
that stores the results of a download operation. It contains:
status
: The final status of the download (from theDownloadStatus
enum).path
: The path to the downloaded file.
Dependencies
This repository uses the following external libraries:
urllib3
: For making HTTPS requests.- truststore: For SSL context creation and management.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sema4ai_http_helper-1.0.2.tar.gz
.
File metadata
- Download URL: sema4ai_http_helper-1.0.2.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d40f1e324116799635c1f28960af2ee0538dbfe8e907440d034b5b401ecbed5d |
|
MD5 | 90fafcea3c78d771e4266ec10e1d0cbd |
|
BLAKE2b-256 | 0d301d2cd9250f0090215326e0471142194ca7312321d5a1eb8744a55a88552e |
File details
Details for the file sema4ai_http_helper-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: sema4ai_http_helper-1.0.2-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.8.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfe9f218281bb0d15ed1af39c2107a0399312a55c77cbed39c4eb37c867f9dea |
|
MD5 | 400cec5c7c4b963508a7901b6d72a056 |
|
BLAKE2b-256 | 341d2364f1d0884515330a9318b75a96e79583eefb2f051fa4fc5ccb9d7d5954 |