来自python脚本的{urllib2.URLError

时间:2016-01-08 14:45:03

标签: python

我正在运行一个python脚本,它检查我服务器上的文本文件,然后根据它读取的内容打开一个继电器。如果在延迟一段时间后打开继电器,脚本会发送GET请求来更新服务器上的文本文件,然后关闭继电器。 程序运行一段时间都没问题。我可以打开和关闭继电器,当时间延迟超时时它会更新服务器。 如果我让程序继续运行并且在一段时间后什么都不做,我会收到以下错误。

Traceback (most recent call last):
File "remote-relay.py", line 41, in <module>
  readWeb ()
File "remote-relay.py", line 34, in readWeb
  relaystatus = urllib2.urlopen(url).read(1)
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 431, in open
  response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 449, in _open
  '_open', req)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
  result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1227, in http_open
  return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
  raise URLError(err)
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>

这是python脚本

import urllib2
import wiringpi2
import time

url = "http://www.stevesolarhome.com/WaterControl.txt"
url2 = "http://www.stevesolarhome.com/water/water.php?status=0"
wiringpi2.wiringPiSetupGpio()
wiringpi2.pinMode(17,1)
wiringpi2.pinMode(27,1)
wiringpi2.pinMode(22,1)
wiringpi2.digitalWrite(17,1)
wiringpi2.digitalWrite(27,1)
wiringpi2.digitalWrite(22,1)
def requestOFF ():
    request = urllib2.urlopen(url2).read(1)
def delay ():
    counter = 0
    while counter < 400:
        counter += 1 
        time.sleep(2)
            relaystatus = urllib2.urlopen(url).read(1)
            if relaystatus == "0":
            waterOFF ()
            counter = 400
        time.sleep(2)
    requestOFF ()
    time.sleep(3)
def waterOn ():
    wiringpi2.digitalWrite(17,0)
    delay ()        
def waterOFF ():
    wiringpi2.digitalWrite(17,1)
def readWeb ():
    relaystatus = urllib2.urlopen(url).read(1)
    if relaystatus == "1":
        waterOn ()
    elif relaystatus == "0":
        waterOFF ()
    time.sleep(2)
while 1:
    readWeb ()  

由于URL存在而无法找到URL,程序在开始时运行正常。

脚本是错误的,还是经常要求数据

1 个答案:

答案 0 :(得分:0)

听起来像你的互联网连接或DNS解析随机死亡。

&#34;名称或服务未知&#34;由于某种原因,无法通过DNS解析URL。