top of page
  • Seema Nair

Handling Cookies in Locust tests

Locust is a load testing and performance testing tool. It is intended for load testing websites to determine how many concurrent users a system can handle. It is a very effective tool in testing secure websites that employ cookies to enable security.

Locust has built-in client instances for all GET and POST requests. These client instances are created when Locust is instantiated. The cookies are handled by the HttpSession class of the tool. They take care of keeping the session alive between HTTP requests.

While I was executing Locust tests on an application that extensively used cookies, the tests failed after the first two requests were made. The failures occurred because the HTTPS connection was broken due to an error:

requests connectionerror: ('connection aborted.', error(54, 'connection reset by peer')).

An online search on this error yielded general solutions relating to upgrading Python, updating SSL versions or steps to be taken to handle SSL connections in Mac. None of them could resolve the connection reset error.

I modified the script and used requests.Session() instead of client instances to invoke GET/POST requests. Resultantly the HTTPS connection error ceased to appear and the execution was successful. However, the Locust execution results were not displayed on the Dashboard UI. To resolve the problem, requests.Session() was substituted with HttpSession and henceforth UI was updated with appropriate results.

Original Code:

executor =

if method == 'PUT':

executor = self.client.put

elif method == 'GET':

executor = self.client.get

Final Code:

import requests

from locust.clients import HttpSession

self.requests = HttpSession(consumer_cfg.rest_api_url)

executor =

if method == 'PUT':

executor = self.requests.put

elif method == 'GET':

executor = self.requests.get

2,286 views0 comments
bottom of page