Python3.5:在streads中发送数据

2024-06-28 20:44:58 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:我更改了标题和问题,以便与我的问题更相关:

我尝试使用套接字连接在服务器和客户机之间交换数据(主要是术语)。交换数据的大小并不总是相同的,因此我创建了这个类来执行以下操作:

你知道吗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

(我添加了一些打印以查看程序开始出错的地方)

现在,当我启动此服务器时:

你知道吗服务器.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()

然后我尝试加入这个客户:

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

在客户端:

1
00
00
28
28
2

我认为当服务器更改class属性时“已发送Reseau.msg\u“使用classmethod时”重新研究数据()“从bsfc.py公司文件,它修改线程内的版本,但不修改“真实”值。我说的对吗?你知道吗


Tags: importselfclientdataconnectmsgsocketsent