2016-08-08 33 views
-16
import light 
import urllib2 

true = 1 
while(true): 
       try: 
         response = urllib2.urlopen('http://192.168.1.6/light.php') 
         status = response.read() 
       except urllib2.HTTPError, e: 
             print e.code 

       except urllib2.URLError, e: 
             print e.args 

       print status 
       if status=='17': 
         light.lighton(); 
       elif status=='0': 
         light.lightoff(); 
+1

如果你能提供有關目標的功能和發生的錯誤輸入,這將使它很容易幫助您。 –

+0

請注意縮進。爲每個封裝提供一個標籤空間 – Mechanic

+0

請檢查此鏈接 - http://stackoverflow.com/help/how-to-ask –

回答

2

狂猜:你的響應以換行符結束。試試這個:

  status = status.strip() 
      print status 
      if status=='17': 
        light.lighton(); 
      elif status=='0': 
        light.lightoff(); 
+0

感謝它的作品 –

+0

請解釋爲什麼使用strip(); –

+0

因爲您的輸入以換行符結束。 'str.strip()'將刪除終端換行符。 –