python 数据包ASCII码与字符串互转

1、字符串转bytes

    date = datetime.datetime.now().date()
    time_stamp = (datetime.datetime.now() + datetime.timedelta(hours=-2)).strftime('%a, %d %b %Y %H:%M:%S')
    body = '{result:0,timeStamp:"%s","testing": {"name":  "xiaoming"}\n}' % date
    length = 'Content-Length: ' + str(len(body)) + '\r\n'
    Date = 'Date: %s GMT\r\n\r\n' % time_stamp
    header = 'HTTP/1.1 200 \r\nContent-Type: text/json;charset=UTF-8\r\n' + length + Date
    message_fromhex=bytes(header + body, encoding='utf-8')

你可能感兴趣的:(python 数据包ASCII码与字符串互转)