Edit : I changed title and question to be more related to my problem :
I try to exchange datas (dictionnaries mostly) between a server and a client, using a socket connexion. Size of exchanged datas is not always the same, so i made this class to do the operation :
bsfc.py:
class Reseau():
msg_sent = False
@classmethod
def snd_data(cls, connexion, data):
print('22')
pdata = pickle.dumps(data)
print('22')
len_msg = len(pdata)
plen = pickle.dumps(len_msg)
print('22')
while cls.msg_sent is not True :
connexion.sendall(plen)
time.sleep(0.1)
cls.msg_sent=False
print('22')
while cls.msg_sent is not True :
connexion.sendall(pdata)
time.sleep(0.1)
cls.msg_sent = False
print('22')
@classmethod
def get_data(cls, connexion):
print('00')
plen_msg=connexion.recv(1024)
cls.msg_sent=True
print('00')
len_msg=pickle.loads(plen_msg)
print('{}'.format(len_msg))
pdata = connexion.recv(len_msg*2)
cls.msg_sent = True
data=pickle.loads(pdata)
print(data)
return data
(i added some print to see where the program starts to bug)
now when i launch this server:
serveur.py
import socket
import select
from threading import Thread
from bsfc import Reseau
class Find_clients(Thread):
def __init__(self):
Thread.__init__(self)
self.connexion_serveur = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connexion_serveur.bind(('', 12800))
self.connexion_serveur.listen(5)
def run(self):
print('serveur connecté')
serveur = True
while serveur :
connexions_demandees, wlist, xlist = select.select([self.connexion_serveur],
[], [], 1)
for connexion in connexions_demandees :
connexion_client, infos_connexion = connexion.accept()
if connexion_client is not None :
msg="connexion acceptee"
print('msg')
Reseau.snd_data(connexion_client, msg)
print('msg')
user=Reseau.get_data(connexion_client)
print('msg')
user['connexion_client']=connexion_client
user['infos_connexion']=infos_connexion
Clients.add_user(user)
if __name__=='__main__' :
serveur=Find_clients()
serveur.start()
serveur.join()
and then i try to join it with this client :
import socket
from bsfc import Menu
from bsfc import Reseau
from tkinter import *
import pickle
class Interface_reseau(Frame):
def __init__(self, wdw, **kwargs):
Frame.__init__(self, wdw, width=500, height=600, bg='black', **kwargs)
self.pack(fill=BOTH, expand=1)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=2)
self.rowconfigure(1, weight=1)
self.rowconfigure(2, weight=1)
self.rowconfigure(3, weight=3)
self.title=Label(self, bg='black', text = " Rejoindre un serveur \n", font="helvetica 16 bold", fg='white')
self.serveur_liste=Listbox(self)
self.title.grid(row=0, column=0)
self.connect()
def connect(self):
connexion_serveur = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect_lbl=Label(self, bg='black', fg='white', text='connecting...')
self.connect_lbl.grid(row=1, column=0)
try :
connexion_serveur.connect(('localhost', 12800))
self.connect_lbl.destroy()
print('1')
msg = Reseau.get_data(connexion_serveur)
print('2')
Reseau.snd_data(connexion_serveur, Menu.USER)
print('3')
self.connect_lbl=Label(self, bg='black', fg='white', text=msg)
self.connect_lbl.grid(row=1, column=0)
except:
self.connect_lbl.destroy()
self.connect_lbl=Label(self, bg='black', fg='white', text='connection failled')
self.connect_lbl.grid(row=1, column=0)
i have this error on server side :
serveur connecté
msg
22
22
22
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "serveur.py", line 50, in run
Reseau.snd_data(connexion_client, msg)
File "/home/bsfc.py", line 149, in snd_data
connexion.sendall(plen)
BrokenPipeError: [Errno 32] Broken pipe
and this on the client side :
1
00
00
28
28
2
I think when the server is changing the class-attribute "Reseau.msg_sent" while using the classmethod "Reseau.snd_data()" imported from the bsfc.py file, it modifies a version inside the Thread but not the "real" value. Am i correct ?