python multithreaded server unable to receive data from client - python

In the frame of our course, our teacher asked us to write a client-server program, where the server split two matrices that it wants to multiply then send them to the client and the client should calculate their part of the result and send it back to the server.
I succeed to divide the matrix and send it to clients but my problem is that my client cannot send back the results to the server. When I try to receive any message at the server-side, my client no longer receives the matrix to compute.
Here is my server code
!/usr/bin/env python
# -*- coding: utf-8 -*-
from socket import socket, AF_INET, SOCK_STREAM, timeout
from threading import Thread
import numpy as np
import pickle
buf = 4096
class ErrorLevels:
OK = "OK"
ERROR = "ERREUR"
class Server(Thread):
def __init__(self):
Thread.__init__(self)
self.socket = socket(AF_INET, SOCK_STREAM)
self.socket.bind(("localhost", 2020))
self.socket.settimeout(0.5)
self.running = False
self.client_pool = []
def client_handling_stopped(self, client, error_level, error_msg):
print("Le gerant de {} s'est arrete avec le niveau d'erreur {} ({})".format(client.address[0],error_level,error_msg))
self.clean_up()
# self.log_connection_amount()
def log_connection_amount(self):
print("Il y a maintenant {} client(s) connecte(s)".format(len(self.client_pool)))
def stop(self):
print("Arrêt du serveur")
for client in self.client_pool:
client.close_connection()
self.running = False
def clean_up(self):
"""
Enleve tous les gérants de clients innactifs de la liste des gerants de clients
"""
self.client_pool = [client for client in self.client_pool if client.alive]
#le serveur genere le calcul a envoyer aux clients
#generation de matrices
def matrice_aleatoire(self,intervalle, ligne, colonne):
matrice = np.random.randint(intervalle, size=(ligne, colonne))
return matrice
def run(self):
A = self.matrice_aleatoire(10,100,100)
B = self.matrice_aleatoire(10,100,100)
#code fonctionnnant pour 10 clients
#division de A en 10 sous matrices de 10 lignes et envoie aux clients
C = np.vsplit(A, 10)
#dictionnaire a envoyer a chaque client
data = []
for i in range(10):
dic = {'num':i,'partA':C[i],'partB':B}
data.append(dic)
print("Démarrage du serveur\nAttente des connexions clients...")
self.running = True
self.socket.listen(5)
i=-1
while self.running:
try:
client, address = self.socket.accept()
i=i+1
except timeout:
continue # on retourne au début de la boucle jusqu'à avoir un client
print("Connexion depuis {}".format(address))
#envoie et reception du calcul aux clients connnectes
#actuellement 10 clients
client_handling = ClientHandling(client, address,data[i], self.client_handling_stopped)
self.client_pool.append(client_handling)
client_handling.start()
# self.log_connection_amount()
#classe d'ojbets thread pour gerer les connections clients
class ClientHandling(Thread):
def __init__(self, client, address,data, exit_callback):
Thread.__init__(self)
self.client = client
self.address = address
self.data = data
self.exit_callback = exit_callback # une fonction qui devra être appelée lorsque cet objet sera devenu inactif
self.alive = True
def _stop(self, error_level, error_msg):
self.alive = False
self.close_connection()
self.exit_callback(self, error_level, error_msg)
def close_connection(self):
self.alive = False
self.client.close()
print("Fin de la communication avec {}".format(self.address))
def run(self):
try:
#envoie du calcul
print("debut envoie du calcul")
data_string = pickle.dumps(self.data)
self.client.sendall(data_string)
print("fin envoie")
#reception resultat
'''
here is the problem when i try to receive the result
pick_ = b''
while 1:
dat = self.client.recv(buf)
pick_ += dat
print("reception resultat")
if not dat:break
res = pickle.loads(dat)
print("fin reception")
# print(res)'''
#quelques exceptions possibles
except ZeroDivisionError:
self._stop(ErrorLevels.ERROR, "Une division par zero tente")
except ConnectionAbortedError:
if self.alive: # innatendu
self._stop(ErrorLevels.ERROR, "La connexion abandonnee")
else: # on est dans le cas où le gérant est volontairement arrêté
return # on arrête donc tout, plus besoin de faire quoi que ce soit
self._stop(ErrorLevels.OK, "Le client a ferme la connection")
try:
#lancement du thread serveur
server = Server()
server.start()
while True: continue
except KeyboardInterrupt:
server.stop()
server.join()
here is my client.py
import socket
from threading import Thread
#import numpy as np
import pickle
hote = "localhost"
port = 2020
buf = 4096
connexion_avec_serveur = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion_avec_serveur.connect((hote, port))
print("Connexion établie avec le serveur sur le port {}".format(port))
#thread pour le calcul du client
class Calcul(Thread):
def __init__(self):
Thread.__init__(self)
#fonction qui extrait les donnees et multiplie
def multmat(self,data):
num = data['num']
A = data['partA']
B = data['partB']
C = A # B
resul = {'num':num,'partC':C}
return resul
def run(self):
#reception calcul
pick_str = b''
while 1:
data = connexion_avec_serveur.recv(buf)
pick_str += data
if not data:break
#connexion_avec_serveur.close()
dic = pickle.loads(pick_str)
#print(dic)
#calcul du produit
res = self.multmat(dic)
print(res)
#envoie du resultat du calcul
data_string = pickle.dumps(res)
connexion_avec_serveur.sendall(data_string)
cal = Calcul()
cal.start()
cal.join()
connexion_avec_serveur.close()

The main problem is that the client does not know when the complete message from the server has been received. The receiving code expects the server to close the connection before it can process the incoming data. However, the server can not close the connection because it is waiting for the client to send a response over the same connection.
The client is blocked at data = connexion_avec_serveur.recv(buf) until the server closes the connection, or some other network event occurs that severs that connection. Meanwhile the server is also blocked at dat = self.client.recv(buf) awaiting a response from the client - there is a deadlock.
The solution is to arrange for the client to know when it has received the complete message from the server, which means adding some protocol. One way is for the sever to append a sentinel value to signal the end of the message, and for the client to watch for that sentinel. Another way is for the server to prepend to the message the length of the payload, in this case the length of the pickled data, which I show here.
For the client change the run() function:
import struct
def run(self):
# First 4 bytes are the length of the payload
data = connexion_avec_serveur.recv(4)
msglen = struct.unpack('!L', data)[0]
print(f'Length of payload {msglen = }')
payload = []
while msglen > 0:
print(f'{msglen = } calling recv...')
data = connexion_avec_serveur.recv(buf)
print(f'received {len(data)} bytes')
payload.append(data)
msglen -= len(data)
print(f'total bytes read {sum(len(s) for s in payload)}')
dic = pickle.loads(b''.join(payload))
#print(dic)
#calcul du produit
res = self.multmat(dic)
print(res)
#envoie du resultat du calcul
data_string = pickle.dumps(res)
connexion_avec_serveur.sendall(data_string)
And for the server:
import struct
def run(self):
try:
#envoie du calcul
print("debut envoie du calcul")
data = pickle.dumps(self.data)
# prepend message with length of the pickled data
msg = struct.pack(f'!L{len(data)}s', len(data), data)
print(f'sending {len(msg)} bytes to client')
self.client.sendall(msg)
print("fin envoie")
#reception resultat
pick_ = b''
while True:
print('calling recv()')
dat = self.client.recv(buf)
print(f'recv() returned {len(dat)} bytes')
pick_ += dat
print("reception resultat")
if not dat:
break
res = pickle.loads(pick_)
print(f'{res = }')
print("fin reception")
#quelques exceptions possibles
except ZeroDivisionError:
self._stop(ErrorLevels.ERROR, "Une division par zero tente")
except ConnectionAbortedError:
if self.alive: # innatendu
self._stop(ErrorLevels.ERROR, "La connexion abandonnee")
else: # on est dans le cas où le gérant est volontairement arrêté
return # on arrête donc tout, plus besoin de faire quoi que ce soit
self._stop(ErrorLevels.OK, "Le client a ferme la connection")

Related

Python socket error : connexion refused (stack.error errno 111)

I have a code in python to communicate between two virtuals machines.
But I have an error. Connexion refused. are there any problems with the port that I use?
I don't think there is any firewall or anything.
thanks a lot
Here is my code below :
import socket
import subprocess
try:
# on cree un objet socket
# socket.AF_INET : la famille d adresses, ici ce sont des adresses Internet ;
# socket.SOCK_STREAM : le type du socket, SOCK_STREAM pour le protocole TCP.
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# on defini le port
port = 12345
# connection vers le serveur
s.connect(('192.168.1.20', port))
# receive data from the server, 2048 bytes
print s.recv(2048)
# close the connection
s.close()
except:
print("une erreur est survenue...")
and then the code of my other machine
import socket
import subprocess
import os,sys
#==========================================================================================
#creation d'une classe pour rendre le programme oriente objet
class GetInfo:
infos = os.uname()
currentUser = os.getlogin()
rep_actuel = os.getcwd()
myDirectory = os.listdir("/home/debian/Documents")
scanpath = "Liste de tous les repertoires dans Users : \n"
def listInfo(self):
#afficher des informations sur le systeme
return GetInfo.infos
#afficher l'utilisateur actuel
return GetInfo.currentUser
#afficher le path du repertoire actuel
return GetInfo.rep_actuel
def scan(self):
#print "Liste de tous les repertoires dans Users/"
#on liste toutes les directories et sous directories
for file in GetInfo.myDirectory:
GetInfo.scanpath += file + "; \n"
return GetInfo.scanpath
#instance de l objet GetInfo (creation de l'objet)
GetMyInfo = GetInfo()
# afficage des infos de l'objet
#GetMyInfo.listInfo()
#GetMyInfo.scan()
#==========================================================================================
# on cree un objet socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print "Socket successfully created"
# reservation d'un port specifique
port = 12345
# contient le nom d hote et le numero du port
#identifiant le serveur auquel on veut se connecter.
s.bind(('192.168.1.15', port))
print "socket binded to %s" %(port)
# activation du mode ecoute
# avec un nombre maximum de connexions qu il peut recevoir sur ce port sans les accepter
s.listen(5)
print "socket is listening"
GetMyInfo.scan()
#==========================================================================================
# gestion des erreurs
while True:
# on etabli la connection avec le client
client, addr = s.accept()
print 'Got connection from', addr
#client.send("Liste de tous les repertoires dans Users/")
# send the informations about the victim.
client.send(GetMyInfo.scan())
print client.recvfrom(2048)
# on ferme la connection
s.close()
the error says Connexion refused here is the error below
error
Looks like you are connecting the client to one address:
# connection vers le serveur
s.connect(('192.168.1.20', port)) # <-- **.20**
But the server is listening on another:
# contient le nom d hote et le numero du port
#identifiant le serveur auquel on veut se connecter.
s.bind(('192.168.1.15', port)) # <-- **.15**

Socket Problem in Python when send data to server

I have a problem when I send data from client to server in python INET Stream socket.
I send a dictionary using library json of python , and , in the server side, I create a Ticket object and save in my MySQL DB.
At this point, the program don't have any problems, but, when i try save other ticket in the same ejecution of client, the server not recieve the data and , the ticket don't save in the DB. Any solutions?
In server.py
def thread_fuction(port,sock):
while True:
msg = clientsocket.recv(1024)
print(f"Recibido del puerto {port} atendido por PID {os.getpid()}: {msg.decode()}")
logger(sock,msg)
if (msg.decode() == 'INSERTAR'):
dict_data=sock.recv(1024).decode()
final_data=json.loads(dict_data)
final_data=dict(final_data)
for key,value in final_data.items():
if key == "autor":
autor=value
elif key == "titulo":
titulo=value
elif key == "descripcion":
descripcion=value
elif key == "estado":
estado=value
print(final_data)
ticket=Ticket(autor=autor,titulo=titulo,descripcion=descripcion,estado=estado,fecha=datetime.now())
session.add(ticket)
session.commit()
break
# creamos el objeto socket
try:
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print('Fallo al crear el socket!')
sys.exit()
#Establecemos parametros
host = "localhost"
port = int(8070)
# Blindeamos el puerto y el host
serversocket.bind((host, port))
# Establecemos 5 peticiones de escucha como maximo.
serversocket.listen(5)
if __name__ == "__main__":
while True:
# establish a connection
clientsocket, addr = serversocket.accept()
print('Conexion establecida: SERVER ON')
conection=Thread(target=thread_fuction,args=(port,clientsocket))
conection.start()
In client.py
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket Creado!")
except socket.error:
print ('Fallo al crear el socket!')
sys.exit()
host = "localhost"
port = int(8070)
client_socket.connect((host, port))
print ('Socket conectado al host', host, 'en el puerto', port)
while True:
print("""\n
\t\t\t *** Menu ***
- INSERTAR TICKET (INSERTAR)
- SALIR (SALIR)
""")
opcion = input('Opcion: ').upper()
client_socket.sendto(opcion.encode(), (host, port))
if (opcion == 'INSERTAR'):
autor = input("\nIngrese autor del Ticket: ")
titulo = input("\nIngrese titulo del ticket: ")
descripcion = input("\nIngrese descripcion del ticket: ")
estado = input("\nIngrese estado del ticket (pendiente, en procesamiento o resuelto): ")
while validar_estado(estado):
estado=input("Estado debe ser uno de los pedidos, intentelo nuevamente): ")
data={"autor":autor,"titulo":titulo,"descripcion":descripcion,"estado":estado}
json_data=json.dumps(data) #Convertimos el diccionario a JSON
client_socket.sendto(json_data.encode(),(host,port))

I am not able to stream images from a picamera

I found some code here for a project at : https://picamera.readthedocs.io/en/release-1.13/recipes2.html#rapid-capture-and-streaming section 4.9
I successfully make it work but when i tried to put the serveur on the raspberry py instead of the client, i would not work.
EDIT:
EDIT:
I found the answer : On the server the file needs to be open in write (wb) and on the client it needs to be open in read (rb)
However, we get 7 secondes of latency beteen the server and the client, Do you know how i could lower it ?
LE SERVEUR
# -*-coding:utf-8 -*
import io
import socket
import struct
from PIL import Image, ImageTk
from threading import Thread
import time
class ControleurClientVideo():
def __init__(self, controleur_client):
self.adresse='0.0.0.0'
self.port=8000
self._client_socket = socket.socket()
self._connection = None
self._thread = None
self._stop = False
def connection_raspberry(self):
self._thread = Thread(target=self._connection_avec_raspberry)
self._thread.start()
def _connection_avec_raspberry(self):
try:
self._client_socket.connect((self.adresse, self.port))
self._connection = self._client_socket.makefile('wb')
self._connection_active=True
print("Connection avec le serveur etabli")
time.sleep(2)
self._recevoir_flux_image()
except Exception as e:
print(e)
def _recevoir_flux_image(self):
try:
while not (self._stop):
# Read the length of the image as a 32-bit unsigned int. If the
# length is zero, quit the loop
image_len = struct.unpack('<L',
self._connection.read(struct.calcsize('<L')))[0]
if not image_len:
self.connection_perdu = True
break
# Construct a stream to hold the image data and read the image
# data from the connection
image_stream = io.BytesIO()
image_stream.write(self._connection.read(image_len))
# Rewind the stream, open it as an image with PIL and do some
image_stream.seek(0)
image_pill = Image.open(image_stream)
image_pill = image_pill.resize((320, 240), Image.ANTIALIAS)
image_tk = ImageTk.PhotoImage(image_pill)
print(image_tk)
self.controleur_client.changer_image(image_tk)
finally:
self.fermer_connection()
def fermer_connection(self):
self._stop = True
time.sleep(0.5)
if not (self._connection == None):
self._connection.close()
self._connection = None
self._client_socket.close()
self._client_socket=None
self._thread = None
print("Connection avec le serveur fermer")
LE CLIENT
# -*-coding:utf-8 -*
import io
import socket
import struct
import time
import picamera
from threading import Thread
class ControleurStreamingVideo():
def __init__(self):
self.adresse='0.0.0.0'
self.port=8000
self._serveur_socket = socket.socket()
self._serveur_socket.bind((self.adresse, self.port))
self._connection = None
self._thread=None
def ouvrir_serveur(self):
self._thread = Thread(target=self._connection_avec_client)
self._thread.start()
def _connection_avec_client(self):
try:
print("Serveur en attente d'une connection...")
self._serveur_socket.listen(5)
self._connection = self._serveur_socket.accept()[0].makefile('rb')
print("Connection réussi, début de la vidéo")
except Exception as e:
repr(e)
finally:
self._envoit_image()
self._serveur_socket.close()
def _envoit_image(self):
try:
self.output = SplitFrames(self._connection)
with picamera.PiCamera(resolution='VGA', framerate=30) as camera:
time.sleep(1) #warmup la caméra
camera.start_recording(self.output, format='mjpeg')
camera.wait_recording(30)
camera.stop_recording()
self._serveur_socket.close()
except Exception as e:
print(e)
class SplitFrames(object):
def __init__(self, connection):
self.connection = connection
self.stream = io.BytesIO()
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# Start of new frame; send the old one's length
# then the data
size = self.stream.tell()
if size > 0:
self.connection.write(struct.pack('<L', size))
self.connection.flush()
self.stream.seek(0)
self.connection.write(self.stream.read(size))
self.stream.seek(0)
self.stream.write(buf)
When i run the program it says only write for error. I found out that the problem come from the method write in SplitFrames. Any idea on what is causing this
SERVEUR:
Serveur en attente d'une connection...
Connection réussi, début de la vidéo
write
CLIENT:
Connection avec le serveur etabli
Connection avec le serveur fermer
read
EDIT:
I found the answer : On the server the file needs to be open in write (wb) and on the client it needs to be open in read (rb)
However, we get 7 secondes of latency beteen the server and the client, Do you know how i could lower it ?

Client-server python converting bytes to string

I create a client-server model in python.
It's like a chat room, but i tried to encrypt the messages using "CTR" from cryptography.io.
When i encrypt and decrypt in the client, it work pretty well but when i sent it to the server always showing this :
> > Task exception was never retrieved
> future: <Task finished coro=<handle_echo() done, defined at Server.py:43> exception=UnicodeDecodeError('utf-8', b'\x8f.\xcb', 0,
> 1, 'invalid start byte')>
> Traceback (most recent call last):
> File "Server.py", line 53, in handle_echo
> data = srvwrk.process(data)
> File "Server.py", line 25, in process
> txt = msg.decode()
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8f in position 0: invalid start byte
Sorry about my english, thanks
client:
# Código baseado em https://docs.python.org/3.6/library/asyncio-stream.html#tcp-echo-client-using-streams
import asyncio
import socket
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
conn_port = 8888
max_msg_size = 9999
class Client:
""" Classe que implementa a funcionalidade de um CLIENTE. """
def __init__(self, sckt=None):
""" Construtor da classe. """
self.sckt = sckt
self.msg_cnt = 0
def process(self, msg=b""):
""" Processa uma mensagem (`bytestring`) enviada pelo SERVIDOR.
Retorna a mensagem a transmitir como resposta (`None` para
finalizar ligação) """
self.msg_cnt +=1
backend = default_backend()
key = os.urandom(32)
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CTR(iv), backend=backend)
print('Received (%d): %r' % (self.msg_cnt , msg.decode()))
print('Input message to send (empty to finish)')
new_msg = input().encode()
encryptor = cipher.encryptor()
ct = encryptor.update(new_msg) + encryptor.finalize()
print(ct)
return ct if len(ct)>0 else None
#
#
# Funcionalidade Cliente/Servidor
#
# obs: não deverá ser necessário alterar o que se segue
#
#asyncio.coroutine
def tcp_echo_client(loop=None):
if loop is None:
loop = asyncio.get_event_loop()
reader, writer = yield from asyncio.open_connection('127.0.0.1',
conn_port, loop=loop)
addr = writer.get_extra_info('peername')
client = Client(addr)
msg = client.process()
while msg:
writer.write(msg)
msg = yield from reader.read(max_msg_size)
if msg :
msg = client.process(msg)
else:
break
writer.write(b'\n')
print('Socket closed!')
writer.close()
def run_client():
loop = asyncio.get_event_loop()
loop.run_until_complete(tcp_echo_client())
run_client()
This is the server :
i tried to put 'utf-8' in txt = msg.decode()... but always showing the same error
# Código baseado em https://docs.python.org/3.6/library/asyncio-stream.html#tcp-echo-client-using-streams
import asyncio
import codecs
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
conn_cnt = 0
conn_port = 8888
max_msg_size = 9999
class ServerWorker(object):
""" Classe que implementa a funcionalidade do SERVIDOR. """
def __init__(self, cnt, addr=None):
""" Construtor da classe. """
self.id = cnt
self.addr = addr
self.msg_cnt = 0
def process(self, msg):
""" Processa uma mensagem (`bytestring`) enviada pelo CLIENTE.
Retorna a mensagem a transmitir como resposta (`None` para
finalizar ligação) """
self.msg_cnt += 1
txt = msg.decode()
print(txt)
decryptor = cipher.decryptor()
ctt = decryptor.update(msg) + decryptor.finalize()
print(ctt)
print('%d : %r' % (self.id,txt))
new_msg = txt.upper().encode()
return new_msg if len(new_msg)>0 else None
#
#
# Funcionalidade Cliente/Servidor
#
# obs: não deverá ser necessário alterar o que se segue
#
#asyncio.coroutine
def handle_echo(reader, writer):
global conn_cnt
conn_cnt +=1
addr = writer.get_extra_info('peername')
srvwrk = ServerWorker(conn_cnt, addr)
data = yield from reader.read(max_msg_size)
while True:
if not data: continue
if data[:1]==b'\n': break
data = srvwrk.process(data)
if not data: break
writer.write(data)
yield from writer.drain()
data = yield from reader.read(max_msg_size)
print("[%d]" % srvwrk.id)
writer.close()
def run_server():
loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_echo, '127.0.0.1', conn_port, loop=loop)
server = loop.run_until_complete(coro)
# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
print(' (type ^C to finish)\n')
try:
loop.run_forever()
except KeyboardInterrupt:
pass
# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()
print('\nFINISHED!')
run_server()
You seem to encode then encrypt the message but try to decode then decrypt the message. If you decrypt the message first then decode it this should solve the problem.
I think changing the decryption code to this should do the trick:
decryptor = cipher.decryptor()
ctt = decryptor.update(msg) + decryptor.finalize()
print(ctt)
txt = cct.decode()
print(txt)

Read and Write Socket-SerialPort use expensive CPU

def threadSocket(Socket, port):
'''
Thread encargado de sotener comunicacion entre Socket cliente y
el puerto serial asignado
Mientras se mantenga activo el thread de recepcion se intentara leer del
puerto serial y enviarlo al socket cliente
Se asume que socket y port son objetos vaidos validos
'''
#Intenta abrir el puerto serial
try:
COM = serial.Serial(port-1)
except:
#Si esta en uso no se encuentra disponible, muestra error
print 'Error: Puerto COM'+str(port)+' no disponible'
else:
#Al conectarse se crea el thread de recepcion
print 'Conectado a puerto COM'+str(port)
rxThread = threading.Thread(target=rxThreadDef,args=(Socket,COM))
rxThread.start()
dato = ''
#Mientras el thread este activo
while rxThread.isAlive():
try:
#Lee del puerto serial y envia a Socket cliente
dato += COM.read()
if parserCOMToSocket(dato):
Socket.send(dato)
dato=''
except:
break
print "Puerto cerrado!"
#Cierra puerto Serial
COM.close()
#Cierra Socket
Socket.close()
#print "Hilo terminado"
def rxThreadDef(Socket,COM):
'''
Thread (Hilo) encargado de recibir la informacion del Cliente
y enviarla por medio del puerto serial
Si se genera algun error se rompe el ciclo y termina el thread
'''
dato = ''
while 1:
try:
d = Socket.recv(1024)
except:
break
if d:
dato += d
if parserSocketToCOM(dato):
COM.write(dato)
print dato
dato = ''
else:
break
def parserSocketToCOM(dato):
return True
def parserCOMToSocket(dato):
return True
Hi, I try to implement a bridge between a serial port and a AIR application using sockets in Python 2.7
When the rxThread die, the socketThread will be able to close the socket and the serial Port because the client socket is die. But the script need to wait for a read from the serial port before close all.
If I try with a timeout=0, the CPU usage grow to 100%. If I try with a timeout=None but use COM.isWaiting(), I will get the same problem (100% of CPU)
How can I know if a socket is closed in other way?
If you need to read from two sources, you can do it happily with select(). You won't need threads in that case. select blocks until one or more of the resources is available, and then returns the set of resources available, so you can react on each. Note that select works with file descriptors and you may need to put it in nonblocking mode.

Categories

Resources