2017-04-18 25 views
-1

这里是我的代码Python从Thingspeak给人一种HTTP错误500 intermiittantly

import os 
import glob 
import time 
import sys 
import datetime 
import urllib2 

baseURL = "https://api.thingspeak.com/update?api_key=DCL2IZ1REQT5GUSM" 
os.system('modprobe w1-gpio') 
os.system('modprobe w1-therm') 
base_dir = 'sys/bus/w1/devices/' 

#Temp1 temperature device location 
temp1_file = '/sys/bus/w1/devices/28-0000087787c4/w1_slave' 

#Temp2 temperature Device Location 
temp2_file = '/sys/bus/w1/devices/28-000008762fa3/w1_slave' 

#Determine Temp1 Temperature 
def read_rawtemp_temp1(): 
    y = open(temp1_file,'r') 
    lines = y.readlines() 
    y.close 
    return lines 

def read_temp_temp1(): 
    lines = read_rawtemp_temp1() 
    while lines[0].strip()[-3:] !='YES': 
     time.sleep(0.2) 
     lines = read_rawtemp_temp1() 
    equals_pos = lines[1].find('t=') 

    if equals_pos !=-1: 
     temp_string = lines[1][equals_pos+2:] 
     temp_temp1 = ((float(temp_string)/1000.0)*1.8)-32 
     return temp_temp1 

#Determine Temp2 Temperature 

def read_rawtemp_temp2(): 
    x = open(temp2_file,'r') 
    lines = x.readlines() 
    x.close 
    return lines 

def read_temp_temp2(): 
    lines = read_rawtemp_temp2() 
    while lines[0].strip()[-3:] !='YES': 
     time.sleep(0.2) 
     lines = read_rawtemp_temp1() 

    equals_pos = lines[1].find('t=') 
    if equals_pos !=-1: 
     temp_string = lines[1][equals_pos+2:] 
     temp_temp2 = float(temp_string)/1000.0 
     return temp_temp2 

while True: #Loop 
#Send Temp1 Temperature to Thingspeak 
    temp1_tempin = read_temp_temp1() 

#Send temp2 temperature to Thingspeak 
    temp2_tempin = read_temp_temp2() 

#Pull results together 
    values = [datetime.datetime.now(), temp1_tempin, temp2_tempin] 

#Open Thingspeak channel and assign fields to temperatures 
    j = urllib2.urlopen(baseURL + "&field1=%s" % (temp1_tempin) + "&field2=%s" % (temp2_tempin)) 
try: 
    search_response = urllib2.urlopen(search_request) 
except urllib2.HTTPError: 
    pass 

#Time to next loop 
time.sleep(600) 

以下是错误,这将在几个小时后,停止运行该脚本。

Traceback (most recent call last):
File "tempdatalog.py", line 137, in <module>
j = urllib2.urlopen(baseURL + "&field1=%s" % (temp1_tempin) +
"&field2=%s" % (temp2_tempin))
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 437, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 475, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error

我说:

try: 
    search_response = urllib2.urlopen(search_request) 
except urllib2.HTTPError: 
    pass 

这设法得到它越过错误,但没有奏效。任何建议都会很棒,这只是移动设备中监测医疗系统的开始。

+0

请编辑您的问题以正确格式化代码,它会更容易阅读。请参阅[这里](https://meta.stackexchange.com/help/formatting)了解有关帖子格式的信息。 –

+0

'x.close' does _not_关闭文件'x'。 'x.close()'确实。 – DyZ

+0

您没有在'try'块中包含'j = urllib2.urlopen(...)'。根据你的回溯信息,'HTTPError'是由该行引起的。 –

回答

0

看着你的堆栈跟踪,看起来你在try/except子句中包装了错误的行。

File "tempdatalog.py", line 137, in j = urllib2.urlopen(baseURL + "&field1=%s" % (temp1_tempin) + "&field2=%s" % (temp2_tempin))

该版本至少捕获异常:

#Open Thingspeak channel and assign fields to temperatures 
try: 
    j = urllib2.urlopen(baseURL + "&field1=%s" % (temp1_tempin) + "&field2=%s" % (temp2_tempin)) 
    search_response = urllib2.urlopen(search_request) 
except urllib2.HTTPError: 
    pass 

这仍然没有解释为什么错误出现在首位。我的猜测是限速的一种形式

+0

谢谢你们,我是一名工程师,而不是程序员。但学习新事物永远不会太晚。 –

+0

我试过了,它停止更新thingspeak。我已经完全排除了异常情况,并将睡眠时间改变了很长时间。我知道错误来自他们,我只是不知道为什么。 –