import json
import time
import datetime
import uuid
import hmac
import hashlib
import requests
# apiKey, apiSecret input
apiKey = 'xxx'
apiSecret = 'xxxx'
protocol = 'https'
prefix = ''
def unique_id():
return str(uuid.uuid1().hex)
def get_iso_datetime():
utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone
utc_offset = datetime.timedelta(seconds=-utc_offset_sec)
return datetime.datetime.now().replace(tzinfo=datetime.timezone(offset=utc_offset)).isoformat()
def get_signature(key, msg):
return hmac.new(key.encode(), msg.encode(), hashlib.sha256).hexdigest()
def get_headers(apiKey, apiSecret):
date = get_iso_datetime()
salt = unique_id()
data = date + salt
return {
'Authorization': 'HMAC-SHA256 ApiKey=' + apiKey + ', Date=' + date + ', salt=' + salt + ', signature=' +
get_signature(apiSecret, data),
'Content-Type': 'application/json; charset=utf-8'
}
def getUrl(path):
url = '%s://%s' % (protocol, domain)
if prefix != '':
url = url + prefix
url = url + path
return url
def sendMany(data):
return requests.post(getUrl('/messages/v4/send-many'), headers=get_headers(apiKey, apiSecret), json=data)
# sms send
if __name__ == '__main__':
data = {
'messages': [
{
'to': '01000000000',
'from': '01000000001',
'text': '2차 Test solapi'
}
]
}
res = sendMany(data)
print(json.dumps(res.json(), indent=2, ensure_ascii=False))