2013-10-02 35 views
-1

對不起,我真的是新來的..下面是完整的python腳本。UnicodeEncodeError:'ascii'

該腳本的目的是讀取兩個不同的1線溫度傳感器,然後使用HTTP POST將這些值寫入MySQL數據庫。

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
import requests 
import hashlib 
import time 

#Dont forget to fill in PASSWORD and URL TO saveTemp (twice) in this file 

sensorids = ["28-00000", "28-000004"] 
avgtemperatures = [] 
for sensor in range(len(sensorids)): 
    temperatures = [] 
    for polltime in range(0,3): 
      text = ''; 
      while text.split("\n")[0].find("YES") == -1: 
        # Open the file that we viewed earlier so that python can see what   is in it. Replace the serial number as before. 
        tfile = open("/sys/bus/w1/devices/"+ sensorids[sensor] +"/w1_slave") 
        # Read all of the text in the file. 
        text = tfile.read() 
        # Close the file now that the text has been read. 
        tfile.close() 
        time.sleep(1) 

      # Split the text with new lines (\n) and select the second line. 
      secondline = text.split("\n")[1] 
      # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). 
      temperaturedata = secondline.split(" ")[9] 
      # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. 
      temperature = float(temperaturedata[2:]) 
      # Put the decimal point in the right place and display it. 
      temperatures.append(temperature/1000 * 9.0/5.0 + 32.0) 

    avgtemperatures.append(sum(temperatures)/float(len(temperatures))) 

print avgtemperatures[0] 
print avgtemperatures[1] 

session = requests.Session() 
# Getting a fresh nonce which we will use in the authentication step. 
nonce = session.get(url='http://127.0.0.1/temp/saveTemp.php?step=nonce').text 

# Hashing the nonce, the password and the temperature values (to provide some integrity). 
response = hashlib.sha256('{}PASSWORD{}{}'.format(nonce.encode('utf8'), *avgtemperatures).hexdigest()) 

# Post data of the two temperature values and the authentication response. 
post_data = {'response':response, 'temp1':avgtemperatures[0], 'temp2': avgtemperatures[1]} 

post_request = session.post(url='http://127.0.0.1/temp/saveTemp.php', data=post_data) 

if post_request.status_code == 200 : 
    print post_request.text 

下面是我得到的新錯誤。

Traceback (most recent call last): 
File "/var/www/pollSensors.py", line 42, in <module> 
response = hashlib.sha256('{}PASSWORD{}{}'.format(nonce.encode('utf8'), *avgtemperatures).hexdigest()) 
AttributeError: 'str' object has no attribute 'hexdigest' 
+3

什麼是「nonce」和「avgtemperatures」?請給我們一些我們可以重現您的問題。 –

+0

你的問題完全獨立於MySQL。 –

+0

我將完整的腳本添加到原始文章。 – user2782497

回答

1

nonce是unicode值; session.get(..).text總是unicode。

您試圖強制將該值強制爲字符串而不明確提供編碼。因此,Python正在嘗試使用默認的ASCII編解碼器爲您編碼。該編碼失敗。

改爲將Unicode值明確地編碼爲字符串。對於SHA 256哈希,UTF-8可能沒問題。

response = hashlib.sha256(nonce.encode('utf8') + 'PASSWORD' + 
          str(avgtemperatures[0]) + 
          str(avgtemperatures[1])).hexdigest() 

或使用字符串模板:

response = hashlib.sha256('{}PASSWORD{}{}'.format(
    nonce.encode('utf8'), *avgtemperatures)).hexdigest() 
+0

我添加下面的下面的python腳本和錯誤的改變頂部.. #在/ usr/bin中/ Python的 # - * - 編碼:UTF-8 - * - 新的錯誤: 回溯(最近呼叫最後): 響應=(nonce +'PASSWORD'+ str(avgtemperatures [0])+ str(avgtemperatures [1]))的文件「/var/www/pollSensors.py」,第42行,在 。 hexdigest() AttributeError:'unicode'對象沒有屬性'hexdigest' – user2782497

+0

@ user2782497:編碼標題不是原因,也不是。你在那裏刪除了'hashlib.sha256'。 –

+0

我更新了原帖,顯示更新的代碼和新的錯誤..至少錯誤在改變! – user2782497

0

我得到了類似的問題,但它是小數,而不是ascci

刪除目錄:您-profile.spyder2 \ spyder.lock