Ishant Matlani Ishant Matlani - 7 months ago 77
Python Question

if condition not working in python

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();

Answer

Wild guess: your response is terminated with a newline. Try this:

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