Python - REST API call example with a self certificated SSL connection

less than 1 minute read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import time
import http.client
import ssl
import json
conn = http.client.HTTPSConnection("localhost:9990", timeout=5, context=ssl._create_unverified_context())
csvFileDat = ""
with open("c:\\tmp\\test.csv") as inputFile:
    csvFileDat = inputFile.read()
headers = {
    'content-type': "text/plain",
    'authorization': "Basic base64encoded"
}
sumTotal = 0
repeatCount = 100
for idx in range(repeatCount):
    start = time.time()
    conn.request("PUT", "/rest/import", csvFileDat, headers)
    res = conn.getresponse()
    data = res.read()
    end = time.time()
    elapse = end - start
    sumTotal += elapse
    result = json.loads(data.decode('utf-8'))
print("Total time: {}, Avg: {}".format(sumTotal, sumTotal / repeatCount))