Split requests via requests onto two stages:
- Connect to server (establish TCP and TLS connections).
- Make HTTP request.
It is very useful for all cases when you can prepare your request to server and later you must send request as fast as possible. For example, high frequency trading.
from requests_connection import Session, Connection
# connect (establish TCP and TLS connections)
connection = Connection.https('ya.ru')
# make requests session
session = Session(connection)
# make http request
response = session.get('https://ya.ru/')
You can make only one request via one connection. Reconnect before new request:
connection = Connection.https('ya.ru')
session.connect(connection)
response = session.get('https://ya.ru/')
If connection has been closed, new connection will be created on request automatically:
response = session.get('https://ya.ru/')
response = session.get('https://ya.ru/news/')
Requests to other host or port bypass created connection:
response = session.get('http://ya.ru/')
response = session.get('https://google.com/')
Connection
class help you to create connection. It supports 3 constructors:
Connection.http
-- returns http.client.HTTPConnection.Connection.https
-- returns http.client.HTTPSConnectionConnection.socket
-- also returns http.client.HTTPConnection, but you can setup socket params.
Results:
Requests: 0.362 ± 0.2106
Patched: 0.229 ± 0.03791
Running:
pipenv install --dev
pipenv run python benchmark.py