Python间歇性地从Thingspeak发出HTTP错误500

时间:2017-04-18 23:11:41

标签: python python-2.7 http

这是我的代码

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)

以下是几个小时后停止脚本运行的错误。

  

追踪(最近的呼叫最后):
    文件" tempdatalog.py",第137行,< module>
      j = urllib2.urlopen(baseURL +"& field1 =%s"%(temp1_tempin)+
"& field2 =%s"%(temp2_tempin))
    文件" /usr/lib/python2.7/urllib2.py" ;,第154行,在urlopen中       return opener.open(url,data,timeout)
    文件" /usr/lib/python2.7/urllib2.py" ;,第437行,处于打开状态
      response = meth(req,response)
    文件" /usr/lib/python2.7/urllib2.py",第550行,在http_response中       ' http',请求,响应,代码,消息,hdrs)
    文件" /usr/lib/python2.7/urllib2.py" ;,第475行,出错“       return self._call_chain(* args)
    文件" /usr/lib/python2.7/urllib2.py" ;,第409行,在_call_chain中       result = func(* args)
    文件" /usr/lib/python2.7/urllib2.py",第558行,在http_error_default中       引发HTTPError(req.get_full_url(),代码,msg,hdrs,fp)
  urllib2.HTTPError:HTTP错误500:内部服务器错误

我补充说:

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

这试图让它传递错误,但它没有工作。任何建议都会很棒,这只是移动医疗系统监控器的开始。

1 个答案:

答案 0 :(得分:0)

查看您的堆栈跟踪,看起来您在try/except子句中包含了错误的行。

  

文件“tempdatalog.py”,第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 

这仍然无法解释为什么错误首先出现。我的猜测是速率限制的一种形式