import hashlib
import urllib
import datetime
import json
import requests

def generate_mengwang_pwd(userid, pwd, timestamp):
    """
    add by zhouhl 2017-07-25
    对梦网的用户密码加密
    """
    constant_string = '00000000'
    tmp_string = ''.join([userid.upper(), constant_string, pwd, timestamp])
    print(tmp_string)

    md5 = hashlib.md5()
    md5.update(tmp_string.encode())
    return md5.hexdigest()

def md5test():
    timestamp = datetime.datetime.strftime(
        datetime.datetime.now(), '%m%d%H%M%S')
    ciphertext_pwd = generate_mengwang_pwd('JS5112', '858965', timestamp)
    # 梦网的content需要先GBK编码，再urlencode
    multimt = []
    tmp_i = {}
    tmp_i['mobile'] = '18665318962'
    content = '今天天气转凉，大家请注意防寒保暖。'.encode('GBK')
    tmp_content = urllib.parse.urlencode({'content': content})
    tmp_i['content'] = tmp_content.split('=')[-1]
    multimt.append(tmp_i)


    data = {
        'userid': 'JS5112',
        'pwd': ciphertext_pwd,
        'multimt': multimt,
        'timestamp': timestamp,
    }
    jsonStr = json.dumps(data)
    print(jsonStr)
    try:
        response = requests.post(
            'http://61.145.229.28:7902/sms/v2/std/multi_send',
            headers={'Content-Type': 'application/json'},
            data=jsonStr,
        )
        print(response.content.decode('utf-8'))
    except Exception as e:
        print(e)

md5test()