@@ -137,12 +137,6 @@ def __init__(self, proxies=None, apikey=None, validate_status_code=False):
137137 # not very nice, but prevents warnings when accessing the ZAP API via https
138138 requests .packages .urllib3 .disable_warnings (InsecureRequestWarning )
139139
140- # Currently create a new session for each request to prevent request failing
141- # e.g. when polling the spider status
142- #self.session = requests.Session()
143- #if apikey is not None:
144- # self.session.headers['X-ZAP-API-Key'] = apikey
145-
146140 def urlopen (self , url , * args , ** kwargs ):
147141 """
148142 Opens a url forcing the proxies to be used.
@@ -170,11 +164,11 @@ def _request_api(self, url, query=None, method="GET", body=None):
170164
171165 # In theory we should be able to reuse the session,
172166 # but there have been problems with that
173- self . session = requests .Session ()
174- if self .__apikey is not None :
175- self . session .headers ['X-ZAP-API-Key' ] = self .__apikey
167+ with requests .Session () as session :
168+ if self .__apikey is not None :
169+ session .headers ['X-ZAP-API-Key' ] = self .__apikey
176170
177- response = self . session .request (method , url , params = query , data = body , proxies = self .__proxies , verify = False )
171+ response = session .request (method , url , params = query , data = body , proxies = self .__proxies , verify = False )
178172
179173 if (self .__validate_status_code and response .status_code >= 300 and response .status_code < 500 ):
180174 raise Exception ("Non-successful status code returned from ZAP, which indicates a bad request: "
0 commit comments