Use requests.Session for persistent sessions (HTTP keep-alive)
qris opened this issue · comments
This can result in a 10x speedup when querying an HTTPS Jenkins server for large numbers of job statuses, by reusing the existing SSL session to avoid renegotiating the key for each request. The code is simple:
class Jenkins(object):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.session = requests.Session()
def _http_get(self, url, **kwargs):
''' Perform an HTTP GET request.
This will add required authentication and SSL verification arguments.
'''
response = self.session.get(url, auth=self.auth, verify=self.verify_ssl_cert, proxies=self.proxies, **kwargs)
return _validate(response)
def _http_post(self, url, **kwargs):
''' Perform an HTTP POST request.
This will add required authentication and SSL verification arguments.
'''
response = self.session.post(url, auth=self.auth, verify=self.verify_ssl_cert, proxies=self.proxies, **kwargs)
return _validate(response)