Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [Unreleased]
### Fixed
- Ensure `requests.Session` is closed to prevent lingering TCP connections.

## [0.4.0] - 2025-01-20
### Changed
Expand Down
14 changes: 4 additions & 10 deletions src/zapv2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,6 @@ def __init__(self, proxies=None, apikey=None, validate_status_code=False):
# not very nice, but prevents warnings when accessing the ZAP API via https
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

# Currently create a new session for each request to prevent request failing
# e.g. when polling the spider status
#self.session = requests.Session()
#if apikey is not None:
# self.session.headers['X-ZAP-API-Key'] = apikey

def urlopen(self, url, *args, **kwargs):
"""
Opens a url forcing the proxies to be used.
Expand Down Expand Up @@ -170,11 +164,11 @@ def _request_api(self, url, query=None, method="GET", body=None):

# In theory we should be able to reuse the session,
# but there have been problems with that
self.session = requests.Session()
if self.__apikey is not None:
self.session.headers['X-ZAP-API-Key'] = self.__apikey
with requests.Session() as session:
if self.__apikey is not None:
session.headers['X-ZAP-API-Key'] = self.__apikey

response = self.session.request(method, url, params=query, data=body, proxies=self.__proxies, verify=False)
response = session.request(method, url, params=query, data=body, proxies=self.__proxies, verify=False)

if (self.__validate_status_code and response.status_code >= 300 and response.status_code < 500):
raise Exception("Non-successful status code returned from ZAP, which indicates a bad request: "
Expand Down