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

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))

Advertisements
This entry was posted in Programming, python and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s