Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomchristie/httpcore-the-directors-cut
https://github.com/tomchristie/httpcore-the-directors-cut
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/tomchristie/httpcore-the-directors-cut
- Owner: tomchristie
- Created: 2021-06-24T15:03:49.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-09T13:01:58.000Z (about 3 years ago)
- Last Synced: 2024-10-20T09:02:36.147Z (about 2 months ago)
- Language: Python
- Size: 750 KB
- Stars: 5
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
## Quickstart
Installation:
```shell
$ pip install git+https://github.com/tomchristie/httpcore-the-directors-cut
```Send an HTTP request:
```python
import httpcoreresponse = httpcore.request("GET", "https://www.example.com/")
print(response)
#
print(response.status)
# 200
print(response.headers)
# [(b'Accept-Ranges', b'bytes'), (b'Age', b'557328'), (b'Cache-Control', b'max-age=604800'), ...]
print(response.content)
# b'\n\n\nExample Domain\n\n\n ...'
```## Connection pools
The top-level `httpcore.request()` function is provided for convenience. In practice whenever you're working with `httpcore` you'll want to use the connection pooling functionality that it provides.
```python
import httpcorepool = httpcore.ConnectionPool()
response = pool.request("GET", "https://www.example.com/")
```The huge benefit that connection pools provide is that once you've sent an initial request, the connection to the host can usually be reused by subsequent requests.
This requires less resources that having to reconnect on each request, and is far quicker for the following requests.
```python
import httpcore
import timepool = httpcore.ConnectionPool()
for index in range(5):
started = time.perf_counter()
response = pool.request("GET", "https://www.example.com/")
elapsed = time.perf_counter() - started
print(f"Request {index}: {elapsed:.3f} seconds.")# Will print something similar to this:
#
# Request 0: 0.471 seconds.
# Request 1: 0.114 seconds.
# Request 2: 0.115 seconds.
# Request 3: 0.112 seconds.
# Request 4: 0.112 seconds.
```## Requests, responses, and URLs
Request instances in `httpcore` are deliberately simple, and only include the essential information required to represent an HTTP request.
Properties on the request are plain byte-wise representations.
```python
>>> request = httpcore.Request("GET", "https://www.example.com/")
>>> request.method
b"GET"
>>> request.url
httpcore.URL(scheme=b"https", host=b"www.example.com", port=None, target=b"/")
>>> request.headers
[(b'Host', b'www.example.com')]
>>> request.stream```
The interface is liberal in the types that it accepts, but specific in the properties that it uses to represent them. For example, headers may be specified as a dictionary of strings, but internally are represented as a list of `(byte, byte)` tuples.
```python
>>> headers = {"User-Agent": "custom"}
>>> request = httpcore.Request("GET", "https://www.example.com/", headers=headers)
>>> request.headers
[(b'Host', b'www.example.com'), (b"User-Agent", b"custom")]