這裏是我的代碼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
這設法得到它越過錯誤,但沒有奏效。任何建議都會很棒,這只是移動設備中監測醫療系統的開始。
請編輯您的問題以正確格式化代碼,它會更容易閱讀。請參閱[這裏](https://meta.stackexchange.com/help/formatting)瞭解有關帖子格式的信息。 –
'x.close' does _not_關閉文件'x'。 'x.close()'確實。 – DyZ
您沒有在'try'塊中包含'j = urllib2.urlopen(...)'。根據你的回溯信息,'HTTPError'是由該行引起的。 –