编辑:我更改了标题和问题,与我的问题更相关:
我尝试使用套接字连接在服务器和客户端之间交换数据(主要是字典)。交换数据的大小并不总是相同,所以我让这个类进行操作:
bsfc.py:
AIRBRAKE
(我添加了一些打印以查看程序开始出错的地方)
现在,当我启动此服务器时:
serveur.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
然后我尝试将其加入此客户端:
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()
我在服务器端出现此错误:
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)
这在客户端:
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
我认为当服务器更改class-attribute" Reseau.msg_sent"使用classmethod" Reseau.snd_data()"从bsfc.py文件导入,它修改了Thread内部的版本,但没有修改" real"值。我是对的吗?