1+ import urllib .request
2+ import json
3+ import os
4+ import ssl
5+ import time
6+
7+ def allowSelfSignedHttps (allowed ):
8+ # bypass the server certificate verification on client side
9+ if allowed and not os .environ .get ('PYTHONHTTPSVERIFY' , '' ) and getattr (ssl , '_create_unverified_context' , None ):
10+ ssl ._create_default_https_context = ssl ._create_unverified_context
11+
12+ allowSelfSignedHttps (True ) # this line is needed if you use self-signed certificate in your scoring service.
13+
14+ # Request data goes here
15+ # The example below assumes JSON formatting which may be updated
16+ # depending on the format your endpoint expects.
17+ # More information can be found here:
18+ # https://docs.microsoft.com/azure/machine-learning/how-to-deploy-advanced-entry-script
19+
20+ with open ('test cpu/request.json' ) as f :
21+ data = json .load (f )
22+
23+ body = str .encode (json .dumps (data ))
24+
25+ url = 'https://custom-endpoint.centralindia.inference.ml.azure.com/score'
26+ # Replace this with the primary/secondary key, AMLToken, or Microsoft Entra ID token for the endpoint
27+ api_key = 'f8pjDedS03UCIOVtZdr9xPcO3ImPnXF4'
28+ if not api_key :
29+ raise Exception ("A key should be provided to invoke the endpoint" )
30+
31+ # The azureml-model-deployment header will force the request to go to a specific deployment.
32+ # Remove this header to have the request observe the endpoint traffic rules
33+ headers = {'Content-Type' :'application/json' , 'Authorization' :('Bearer ' + api_key ), 'azureml-model-deployment' : 'deployment-1' }
34+
35+ req = urllib .request .Request (url , body , headers )
36+
37+ try :
38+ t1 = time .time ()
39+ response = urllib .request .urlopen (req )
40+ print ("Time taken to get response : " , time .time ()- t1 )
41+ result = response .read ()
42+ # print(result)
43+ except urllib .error .HTTPError as error :
44+ print ("The request failed with status code: " + str (error .code ))
45+
46+ # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
47+ print (error .info ())
48+ print (error .read ().decode ("utf8" , 'ignore' ))
0 commit comments