Reputation: 33
I'm new in python. I've found a simple TCP Server's code and it works. The problem is: i'm not able to make a if...elif...else works. I think it's a mistake of value's type.
Here's my code (the program always do the else part):
while 1:
data = clientsocket.recv(1024)
tokens = data.split(' ',1)
command = tokens[0]
if not command:
break
else:
if command == 1:
try:
camera.start_preview()
time.sleep(10)
camera.stop_preview()
finally:
camera.close()
elif command == 2:
print "Something"
elif command == 3:
print "something else"
else:
print data
print tokens[0]
print command
clientsocket.close()
It give me the result of the last else which is :
2
2
2
Or the number I send. Thanks in advance for your responses !
Upvotes: 0
Views: 273
Reputation: 122024
command
is a string, not an integer; you are comparing:
>>> '2' == 2
False
Instead, use:
...
tokens = data.split(' ', 1)
try:
command = int(tokens[0])
except ValueError:
break
if command == 1:
...
Upvotes: 3