The request.session() works for me:
Time: 1.936s, from start: 1.936s. (this is the first login call)
b'{"uri":"/login-sessions","cookie":"sessionId=QR9DWRy8984EH4OJvvOmLm1KEFCZwysWHfaVLbczblX0JYmGXdG0rZrZRBfgtDv"}'
sessionId=QR9DWRy8984EH4OJvvOmLm1KEFCZwysWHfaVLbczblX0JYmGXdG0rZrZRBfgtDv
Time: 0.029s, from start: 1.970s (these are followup calls)
Time: 0.118s, from start: 2.093s
Time: 0.027s, from start: 2.125s
Time: 0.027s, from start: 2.157s
Time: 0.026s, from start: 2.188s
Time: 0.027s, from start: 2.220s
Time: 0.027s, from start: 2.252s
Time: 0.027s, from start: 2.285s
Time: 0.026s, from start: 2.316s
With the following code:
#!/usr/bin/python3
import requests
import time
import json
r = requests.Session()
url = "https://10.1.254.1/rest/v1/login-sessions"
payload = "{\"userName\": \"admin\", \"password\":\"go-figure\"}"
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
start=time.time()
tstart=time.time()
response = r.request("POST", url, headers=headers, data = payload, verify=False)
print('Time: %0.3fs, from start: %0.3fs' % (time.time()-tstart, time.time()-start))
print(dir(response.elapsed))
print(response.text.encode('utf8'))
cookie=json.loads(response.text)['cookie']
print(cookie)
time.sleep(5)
start+=5
url = "https://10.1.254.1/rest/v1/system"
headers = {
'Cookie': cookie
}
for i in range(1,10):
tstart=time.time()
response = r.request("GET", url, headers=headers, verify=False)
print('Time: %0.3fs, from start: %0.3fs' % (time.time()-tstart, time.time()-start))
# print(response.text.encode('utf8'))
time.sleep(5)
start+=5
I noticed that I had to add the 5 second sleeps in between the calls, otherwise the call lasted 2-5 seconds.