TypeError:必须在散列之前编码Unicode对象... [英] TypeError: Unicode-objects must be encoded before hashing...
问题描述
from base64 import b64encode,b64decode
来自hashlib import sha256
来自urllib import quote_plus,urlencode
来自hmac import HMAC
进口要求
进口json
进口os
进口时间
#Azure IoT Hub
URI ='#######'zh)
KEY ='######'zh。
IOT_DEVICE_ID ='##'
$
POLICY ='iothubowner'
def generate_sas_token():
   到期= 3600
  ttl = time.time()+到期
  sign_key ="%s \ n%d" %((quote_plus(URI)),int(ttl))
  signature = b64encode(HMAC(b64decode(KEY),sign_key,sha256).digest())
  rawtoken = {
      'sr': URI,
      'sig':签名,
      'se':str(int(ttl))
  }
  rawtoken ['skn'] =政策
 返回'SharedAccessSignature'+ urlencode(rawtoken)
$
def send_message(令牌,消息):
< span style ="white-space:pre"> url ='https:// {0} / devices / {1} /messages/events?api-version=2016-11-14'.format(URL ,IOT_DEVICE_ID)
&NBSP; headers = {
&NBSP; &NBSP; &NBSP; "Content-Type":"application / json",
&NBSP; &NBSP; &NBSP; "授权":令牌
&NBSP; }¥b $ b &NBSP; data = json.dumps(消息)
&NBSP;打印数据
&NBSP; response = requests.post(url,data = data,headers = headers)
如果__name__ =='__main__':
&NBSP; #2。生成SAS令牌
&NBSP; token = generate_sas_token()
&NBSP; #3。向物联网中心发送温度
&NBSP;而真实:
&NBSP; &NBSP; &NBSP; message = {" temp":str(12)}
&NBSP; &NBSP; &NBSP; send_message(令牌,留言)
&NBSP; &NBSP; &NBSP; time.sleep(1)
from base64 import b64encode, b64decode
from hashlib import sha256
from urllib import quote_plus, urlencode
from hmac import HMAC
import requests
import json
import os
import time
# Azure IoT Hub
URI = '#######'
KEY = '######'
IOT_DEVICE_ID = '##'
POLICY = 'iothubowner'
def generate_sas_token():
expiry=3600
ttl = time.time() + expiry
sign_key = "%s\n%d" % ((quote_plus(URI)), int(ttl))
signature = b64encode(HMAC(b64decode(KEY), sign_key, sha256).digest())
rawtoken = {
'sr' : URI,
'sig': signature,
'se' : str(int(ttl))
}
rawtoken['skn'] = POLICY
return 'SharedAccessSignature ' + urlencode(rawtoken)
def send_message(token, message):
url = 'https://{0}/devices/{1}/messages/events?api-version=2016-11-14'.format(URI, IOT_DEVICE_ID)
headers = {
"Content-Type": "application/json",
"Authorization": token
}
data = json.dumps(message)
print data
response = requests.post(url, data=data, headers=headers)
if __name__ == '__main__':
# 2. Generate SAS Token
token = generate_sas_token()
# 3. Send Temperature to IoT Hub
while True:
message = { "temp": str(12) }
send_message(token, message)
time.sleep(1)
推荐答案
您好,
您能否提供有关您所遇问题的更多详情面对?你到底想要达到什么目的?您是否关注任何文件?
Can you provide more details about the issue which you are facing? What exactly are you trying to achieve? Are you following any documentation?
这篇关于TypeError:必须在散列之前编码Unicode对象...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!