Skip to content

Instantly share code, notes, and snippets.

@nomad23
Created June 19, 2019 18:25
Show Gist options
  • Save nomad23/5555ea8d9e20766bed9e2b095c06d50f to your computer and use it in GitHub Desktop.
Save nomad23/5555ea8d9e20766bed9e2b095c06d50f to your computer and use it in GitHub Desktop.
# # ########### Python 3.2 #############
# import http.client, urllib.request, urllib.parse, urllib.error, base64
# from flask import Flask
#
# app = Flask(__name__)
# headers = {
# # Request headers
# 'Content-Type': 'application/json',
# 'Ocp-Apim-Subscription-Key': '08c91afcbf394fe1a54e1c855bb1b8c7',
# }
#
# params = urllib.parse.urlencode({
# # Request parameters
# 'visualFeatures': 'Categories',
# 'details': '{string}',
# 'language': 'en',
# })
# @app.route('/post', methods=['POST'])
# def index():
# try:
# conn = http.client.HTTPSConnection('westeurope.api.cognitive.microsoft.com')
# conn.request("POST", "/vision/v1.0/analyze?%s" % params, "{body}", headers)
# response = conn.getresponse()
# data = response.read()
# return data
# conn.close()
# except Exception as e:
# return "[Errno {0}] {1}".format(e.errno, e.strerror)
#
#
# ####################################
#
# if __name__=='__main__':
# app.run(debug=True)
########### Python 3.2 #############
import http.client, urllib.request, urllib.parse, urllib.error, base64, os
headers = {
# Request headers
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': '08c91afcbf394fe1a54e1c855bb1b8c7',
}
params = urllib.parse.urlencode({
# Request parameters
'visualFeatures': 'Categories',
# 'details': '{string}',
'language': 'en',
})
try:
filename = 'jason.jpg'
with open(filename, 'rb') as f:
img_data = f.read()
conn = http.client.HTTPSConnection('westeurope.api.cognitive.microsoft.com')
conn.request("POST", "/vision/v1.0/analyze?%s" % params, img_data, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment