Created
January 27, 2022 02:38
-
-
Save elbruno/eb1776d704882101930e63bdaaabe39e to your computer and use it in GitHub Desktop.
azuremlendpointsampleclient.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
import json | |
import os | |
import ssl | |
import base64 | |
def allowSelfSignedHttps(allowed): | |
# bypass the server certificate verification on client side | |
if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None): | |
ssl._create_default_https_context = ssl._create_unverified_context | |
allowSelfSignedHttps(True) # this line is needed if you use self-signed certificate in your scoring service. | |
# Request data goes here | |
data = { | |
"Inputs": { | |
"WebServiceInput0": | |
[ | |
{ | |
'image': "data:image/png;base64,", | |
'id': "0", | |
'category': "space_wolf", | |
}, | |
], | |
}, | |
"GlobalParameters": { | |
} | |
} | |
# Save string of image file path below | |
img_filepath = "./test_images/squirrel_01.jpg" | |
# Create base64 encoded string | |
with open(img_filepath, "rb") as f: | |
image_string = base64.b64encode(f.read()).decode("utf-8") | |
image_data = str(f"""data:image/png;base64,{image_string}""") | |
data['Inputs']['WebServiceInput0'][0]['image'] = image_data | |
body = str.encode(json.dumps(data)) | |
url = 'http://<ENDPOINT IP ADDRESS>:80/api/v1/service/sq06densenetendpoint/score' | |
api_key = '<API KEY>' # Replace this with the API key for the web service | |
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)} | |
req = urllib.request.Request(url, body, headers) | |
try: | |
response = urllib.request.urlopen(req) | |
result = response.read() | |
print(result) | |
except urllib.error.HTTPError as error: | |
print("The request failed with status code: " + str(error.code)) | |
# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure | |
print(error.info()) | |
print(json.loads(error.read().decode("utf8", 'ignore'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment