I'm working on a python client which is supposed to converse with a C programmed server. For now it just answers a few recognised commands with "ok" (the server part works when I test it with netcat as the client). But my python client only gets the ok 1 out of four times (and empty lines the rest of the time). I don't get why as the socket is set as blocking so I figure it does receive something but I don't know what.
Here is what I have. "info" is just an object with a few strings stored in it, everything before the loop works and a bit simplified for readability : I'm just supposed to receive a welcome message then send back the team name of the connected player then receive a position.
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
sys.exit();
try:
remote_ip = socket.gethostbyname(info.host)
except socket.gaierror:
print "Hostname could not be resolved. Exiting"
sys.exit()
s.setblocking(1)
s.settimeout(60)
s.connect((remote_ip, info.port))
data = s.recv(1024)
print data
team = info.team + "\n"
s.sendall(team)
data = s.recv(1024)
print data
while 42:
msg = raw_input('>> ')
s.sendall(msg + "\n")
data = s.recv(1024)
if data == "":
break
else:
print data
s.shutdown(socket.SHUT_WR)
s.close()