Skip to main content

No project description provided

Project description

hteetp

PyPI - Version PyPI - Python Version

hteetp is a Python package for copying HTTP responses to disk. It provides wrappers around the standard library's http.client infrastructure.

This is useful when making something that should capture traffic, either for later analysis or for a cache.

It's like tee but for HTTP requests.

Installation

pip install hteetp

Usage

The simplest usage is hteetp.http_request:

from hteetp import http_request


def request_and_record():
	response = http_request("./snapshot.http", "GET", "github.com", "/spenczar/hteetp")
	# Now the file ./snapshot.http exists - but it will only have headers and the 
	# 'HTTP/1.1' preamble
	
	response_data = response.read()
	# Now the file will be populated with the contents of response_data
	
	assert len(response.headers) > 0
	assert len(response_data) != 0

Long-lived connections

Alternatively, hteetp.TeeHTTPConnection is a replacement for http.client.HTTPConnection. You provide it with a function for generating filepaths given HTTP request parameters, and it manages the teeing for you. This gives you a long-lived connection if you're hitting the same host many times.

from hteetp import TeeHTTPConnection, relative_files


def request_and_record():
	conn = TeeHTTPConnection(relative_files("./snapshots") "github.com")
	for path in ["/spenczar/hteetp", "/python/cpython", "/psf/requests"]:
		resp = conn.request("GET", path)
		resp_data = resp.read()
		# handle each response
	# The following files will have been written:
	# ./snapshots/GET/github.com:443/spenczar/hteetp
	# ./snapshots/GET/github.com:443/python/cpython	
	# ./snapshots/GET/github.com:443/psf/requests	

Reading HTTP from files on disk

hteetp.http_response_from_file is a little utility for loading a http.client.HTTPResponse from a file on disk:

from hteetp import http_response_from_file

resp = http_response_from_file("./snapshots/GET/github.com:443/spenczar/hteetp")
print(resp.headers)
resp_body = resp.read()
print(resp_body)

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distribution

hteetp-0.1.tar.gz (9.0 MB view hashes)

Uploaded Source

Built Distribution

hteetp-0.1-py3-none-any.whl (8.0 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