'Python / tcp / ascii / How to be able to send an ascii command to a server while continuously receiving incoming ascii string from the server?
I'm working on a project related with some naval radar and how to use the data that its internal "server" sends while rotating about other ships.
the documentation for the radar protocol: here
The main idea is to create a client application to receive the data sent from the radar server, which are ascii sentences containing other ship's information, like speed, position, etc. those are always being sent on a tcp connection.
at the same time, the client must be able to send command in the same format, something like:
<command>[,<argument#1>][,<argument#2>][,...][,<argument#n>]<eol>
to ask for a specific ship information for example.
so all I have until now is this basic client (please dismiss the nonsense #coments on it)
import socket
import time
#try:
#ip = raw_input('IP: ')
#except:
#ip = input('IP: ')
#puerto = input("PUERTO??")
#MESSAGE=b'#000\r'
ip = '127.0.0.1'
puerto = 1235
try:
srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print('Failed to create socket')
sys.exit()
print('Socket Created')
srvsock.connect((ip, puerto))
srvsock.setblocking(0)
print ('Conectado a ' + ip + ':' + str(puerto))
while True:
#listening and receiving ascii strings (buf) from radar
try:
buf = srvsock.recv(1024)
if buf == 'stop':
break
elif len(buf) > 0:
print(buf)
except:
print('error de cliente')
break
# how to be able to send commands while listening the radar??
#try:
#msg = raw_input ('Mensage: ')
#msgencode = msg.encode ('ascii')
#srvsock.send(msgencode)
#except socket.error:
#print('Failed to send data')
#break
srvsock.close()
So the idea is to be listening all the time the incoming ascii strings sent by the server... and also being able to send commands from the client app to the sever to request more data.
I'm still working on this but any better or professional way of doing this would be really appreciated. I'm a little bit confuse with the implementation of a non blocking code to do this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
