【python】UI/ function code seperated(ISP), TCP server receive multi client and send to a client

这是一个使用PyQt5构建的多线程TCP服务器示例,实现了UI与功能代码的分离。服务器能接收多个客户端连接,并通过独立线程发送消息到特定IP的客户端。通信通过事件队列实现。
摘要由CSDN通过智能技术生成

【python】UI/ function code seperated(ISP), multithread TCP server receive multi client and send to specific one client

少少行數實現的python tcp-server,自愉自樂用 :)

This example mention on:

  1. ISP:pyqt5 Ui_form class and function code in different class
  2. Use ThreadingTcpServer class not QTcpSocket class
    (if you don’t need Graphic interface, this example can actually remove QT libraries)
  3. Send by independent thread, received in ThreadingTcpServer handle
  4. cross-threading communication use Queue

Graphic user interface

在这里插入图片描述

explain each thread 類別介紹

main thread of this application

class TcpServer_Tool(QWidget, Ui_Form):

tcp-server thread

class client_handler(BaseRequestHandler):

thread to watch number of connected clients

class check_socket_sum(threading.Thread):

thread to send message to one client by IP

class client_sendBack(threading.Thread): 

explain each queue 事件隊列

client_msg_queue = queue.Queue()
client_add_addr_queue = queue.Queue()
client_remove_addr_queue =  queue.Queue()
client_recv_queue =queue.Queue()
client_send_msg_queue = queue.Queue()

client_socket_list = []

如下:

name description
client_msg_queue for client connect/ disconnect action signal
client_add_addr_queue/ client_remove_addr for user interface to add client list
client_recv_queue content received from client
client_send_msg_queue user send to specific clients from server

源碼

tcpserver_tool.py

Graphic User interface:
(translated from PyQt5 .ui file format)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'tcpServer_tool.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1078, 728)
        font = QtGui.QFont()
        font.setFamily("Century Gothic")
        font.setPointSize(11)
        Form.setFont(font)
        self.gridLayout = QtWidgets.QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.groupBox = QtWidgets.QGroupBox(Form)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(2)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth())
        self.groupBox.setSizePolicy(sizePolicy)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setObjectName("label")
        self.gridLayout_2.addWidget(self.label, 0, 0, 1, 1)
        self.s1_line_1 = QtWidgets.QLineEdit(self.groupBox)
        self.s1_line_1.setObjectName("s1_line_1")
       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值