# -*- coding:utf-8 -*-
import sys
import os
import ssl
import base64
import json
import image_sdk.ais as ais
if sys.version_info.major < 3:
import urllib
import urllib2
def request_token(_url, _data, token):
kreq = urllib2.Request(url=_url)
kreq.add_header('Content-Type', 'application/json')
kreq.add_header('X-Auth-Token', token)
kreq.add_data(json.dumps(_data))
resp = None
status_code = None
try:
#
# Here we use the unvertified-ssl-context, Because in FunctionStage
# the client CA-validation have some problem, so we must do this.
#
_context = ssl._create_unverified_context()
r = urllib2.urlopen(kreq, context=_context, timeout=5)
#
# We use HTTPError and URLError,because urllib2 can't process the 4XX &
# 500 error in the single urlopen function.
#
# If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
# there is no this problem.
#
except urllib2.HTTPError as e:
resp = e.read()
status_code = e.code
except urllib2.URLError as e:
resp = e.reason
else:
status_code = r.code
resp = r.read()
return status_code, resp
def request_job_result_token(_url, token):
kreq = urllib2.Request(url=_url)
kreq.add_header('X-Auth-Token', token)
kreq.add_header('Content-Type', 'application/json')
resp = None
status_code = None
try:
#
# Here we use the unvertified-ssl-context, Because in FunctionStage
# the client CA-validation have some problem, so we must do this.
#
_context = ssl._create_unverified_context()
r = urllib2.urlopen(kreq, context=_context, timeout=5)
#
# We use HTTPError and URLError,because urllib2 can't process the 4XX &
# 500 error in the single urlopen function.
#
# If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
# there is no this problem.
#
except urllib2.HTTPError as e:
resp = e.read()
status_code = e.code
except urllib2.URLError as e:
resp = e.reason
else:
status_code = r.code
resp = r.read()
return status_code, resp
def request_aksk(sig, kreq, _url):
resp = None
status_code = None
try:
sig.Sign(kreq)
#
# Here we use the unvertified-ssl-context, Because in FunctionStage
# the client CA-validation have some problem, so we must do this.
#
_context = ssl._create_unverified_context()
req = urllib2.Request(url=_url, data=kreq.body, headers=kreq.headers)
r = urllib2.urlopen(req, context=_context, timeout=5)
#
# We use HTTPError and URLError,because urllib2 can't process the 4XX &
# 500 error in the single urlopen function.
#
# If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
# there is no this problem.
#
except urllib2.HTTPError as e:
resp = e.read()
status_code = e.code
except urllib2.URLError as e:
resp = e.reason
else:
status_code = r.code
resp = r.read()
return status_code, resp
def request_job_result_aksk(sig, kreq, _url):
resp = None
status_code = None
try:
sig.Sign(kreq)
#
# Here we use the unvertified-ssl-context, Because in FunctionStage
# the client CA-validation have some problem, so we must do this.
#
_context = ssl._create_unverified_context()
req = urllib2.Request(url=_url, headers=kreq.headers)
r = urllib2.urlopen(req, context=_context, timeout=5)
#
# We use HTTPError and URLError,because urllib2 can't process the 4XX &
# 500 error in the single urlopen function.
#
# If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
# there is no this problem.
#
except urllib2.HTTPError as e:
resp = e.read()
status_code = e.code
except urllib2.URLError as e:
resp = e.reason
else:
status_code = r.code
resp = r.read()
return status_code, resp
def download_url_base64(url):
return base64.b64encode(urllib.urlopen(url).read())
else:
import socket
import urllib.parse
import urllib.request
from urllib.error import URLError, HTTPError
socket.setdefaulttimeout(5)
def request_token(_url, _data, token):
_headers = {
"Content-Type": "application/json",
"X-Auth-Token": token
}
data = bytes(json.dumps(_data), 'utf8')
kreq = urllib.request.Request(_url, data, _headers)
resp = None
status_code = None
try:
#
# Here we use the unvertified-ssl-context, Because in FunctionStage
# the client CA-validation have some problem, so we must do this.
#
r = urllib.request.urlopen(kreq)
#
# We use HTTPError and URLError,because urllib2 can't process the 4XX &
# 500 error in the single urlopen function.
#
# If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
# there is no this problem.
#
except HTTPError as e:
resp = e.read()
status_code = e.code
except URLError as e:
resp = e.reason
else:
status_code = r.code
resp = r.read()
return status_code, resp
def request_job_result_token(_url, token):
_headers = {
"Content-Type": "application/json",
"X-Auth-Token": token
}
kreq = urllib.request.Request(_url, headers=_headers)
resp = None
status_code = None
try:
#
# Here we use the unvertified-ssl-context, Because in FunctionStage
# the client CA-validation have some problem, so we must do this.
#
_context = ssl._create_unverified_context()
r = urllib.request.urlopen(kreq, context=_context)
#
# We use HTTPError and URLError,because urllib can't process the 4XX &
# 500 error in the single urlopen function.
#
# If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
# there is no this problem.
#
except HTTPError as e:
resp = e.read()
status_code = e.code
except URLError as e:
resp = e.reason
else:
status_code = r.code
resp = r.read()
return status_code, resp.decode('utf-8')
def request_aksk(sig, kreq, _url):
resp = None
status_code = None
try:
sig.Sign(kreq)
#
# Here we use the unvertified-ssl-context, Because in FunctionStage
# the client CA-validation have some problem, so we must do this.
#
_context = ssl._create_unverified_context()
req = urllib.request.Request(url=_url, data=kreq.body, headers=kreq.headers)
r = urllib.request.urlopen(req, context=_context)
#
# We use HTTPError and URLError,because urllib can'
评论0