python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例


Posted in Python onJune 17, 2019

本文在上文的基础上重新实现支持多线程的服务器。

以下为TCP客户端的程序代码:

#!/usr/bin/env python3

import sys
from PyQt5.QtCore import (QByteArray, QDataStream, QDate, QIODevice,
    QRegExp, Qt)
from PyQt5.QtWidgets import (QApplication, QDateEdit, QFrame, QGridLayout,
    QHBoxLayout, QLabel, QLineEdit, QPushButton,
    QWidget)
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtNetwork import (QTcpSocket,)

MAC = True
try:
  from PyQt5.QtGui import qt_mac_set_native_menubar
except ImportError:
  MAC = False

PORT = 9407
SIZEOF_UINT16 = 2


class BuildingServicesClient(QWidget):

  def __init__(self, parent=None):
    super(BuildingServicesClient, self).__init__(parent)

    self.socket = QTcpSocket()
    self.nextBlockSize = 0
    self.request = None

    roomLabel = QLabel("&Room")
    self.roomEdit = QLineEdit()
    roomLabel.setBuddy(self.roomEdit)
    regex = QRegExp(r"[0-9](?:0[1-9]|[12][0-9]|3[0-4])")
    self.roomEdit.setValidator(QRegExpValidator(regex, self))
    self.roomEdit.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
    dateLabel = QLabel("&Date")
    self.dateEdit = QDateEdit()
    dateLabel.setBuddy(self.dateEdit)
    self.dateEdit.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
    self.dateEdit.setDate(QDate.currentDate().addDays(1))
    self.dateEdit.setDisplayFormat("yyyy-MM-dd")
    responseLabel = QLabel("Response")
    self.responseLabel = QLabel()
    self.responseLabel.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken)

    self.bookButton = QPushButton("&Book")
    self.bookButton.setEnabled(False)
    self.unBookButton = QPushButton("&Unbook")
    self.unBookButton.setEnabled(False)
    quitButton = QPushButton("&Quit")
    if not MAC:
      self.bookButton.setFocusPolicy(Qt.NoFocus)
      self.unBookButton.setFocusPolicy(Qt.NoFocus)

    buttonLayout = QHBoxLayout()
    buttonLayout.addWidget(self.bookButton)
    buttonLayout.addWidget(self.unBookButton)
    buttonLayout.addStretch()
    buttonLayout.addWidget(quitButton)
    layout = QGridLayout()
    layout.addWidget(roomLabel, 0, 0)
    layout.addWidget(self.roomEdit, 0, 1)
    layout.addWidget(dateLabel, 0, 2)
    layout.addWidget(self.dateEdit, 0, 3)
    layout.addWidget(responseLabel, 1, 0)
    layout.addWidget(self.responseLabel, 1, 1, 1, 3)
    layout.addLayout(buttonLayout, 2, 1, 1, 4)
    self.setLayout(layout)

    self.socket.connected.connect(self.sendRequest)
    self.socket.readyRead.connect(self.readResponse)
    self.socket.disconnected.connect(self.serverHasStopped)
    #self.connect(self.socket,
    #       SIGNAL("error(QAbstractSocket::SocketError)"),
     #      self.serverHasError)
    self.socket.error.connect(self.serverHasError)
    self.roomEdit.textEdited.connect(self.updateUi)
    self.dateEdit.dateChanged.connect(self.updateUi)

    self.bookButton.clicked.connect(self.book)
    self.unBookButton.clicked.connect(self.unBook)
    quitButton.clicked.connect(self.close)

    self.setWindowTitle("Building Services")


  def updateUi(self):
    enabled = False
    if (self.roomEdit.text() and
      self.dateEdit.date() > QDate.currentDate()):
      enabled = True
    if self.request is not None:
      enabled = False
    self.bookButton.setEnabled(enabled)
    self.unBookButton.setEnabled(enabled)


  def closeEvent(self, event):
    self.socket.close()
    event.accept()


  def book(self):
    self.issueRequest("BOOK", self.roomEdit.text(),
             self.dateEdit.date())


  def unBook(self):
    self.issueRequest("UNBOOK", self.roomEdit.text(),
             self.dateEdit.date())


  def issueRequest(self, action, room, date):
    self.request = QByteArray()
    stream = QDataStream(self.request, QIODevice.WriteOnly)
    stream.setVersion(QDataStream.Qt_5_7)
    stream.writeUInt16(0)
    stream.writeQString(action)
    stream.writeQString(room)
    stream << date
    stream.device().seek(0)
    stream.writeUInt16(self.request.size() - SIZEOF_UINT16)#overwrite seek(0)
    self.updateUi()
    if self.socket.isOpen():
      self.socket.close()
    self.responseLabel.setText("Connecting to server...")
    self.socket.connectToHost("localhost", PORT)


  def sendRequest(self):
    self.responseLabel.setText("Sending request...")
    self.nextBlockSize = 0
    self.socket.write(self.request)
    self.request = None


  def readResponse(self):
    stream = QDataStream(self.socket)
    stream.setVersion(QDataStream.Qt_5_7)

    while True:
      if self.nextBlockSize == 0:
        if self.socket.bytesAvailable() < SIZEOF_UINT16:
          break
        self.nextBlockSize = stream.readUInt16()
      if self.socket.bytesAvailable() < self.nextBlockSize:
        break
      action = ""
      room = ""
      date = QDate()
      #stream >> action >> room
      action=stream.readQString()
      room=stream.readQString()
      if action != "ERROR":
        stream >> date
      if action == "ERROR":
        msg = "Error: {0}".format(room)
      elif action == "BOOK":
        msg = "Booked room {0} for {1}".format(room,date.toString(Qt.ISODate))
      elif action == "UNBOOK":
        msg = "Unbooked room {0} for {1}".format(room,date.toString(Qt.ISODate))
      self.responseLabel.setText(msg)
      self.updateUi()
      self.nextBlockSize = 0


  def serverHasStopped(self):
    self.responseLabel.setText(
        "Error: Connection closed by server")
    self.socket.close()


  def serverHasError(self, error):
    self.responseLabel.setText("Error: {0}".format(self.socket.errorString()))
    self.socket.close()


app = QApplication(sys.argv)
form = BuildingServicesClient()
form.show()
app.exec_()

以下为TCP服务端的程序代码:

#!/usr/bin/env python3
import bisect
import collections
import sys
from PyQt5.QtCore import (QByteArray, QDataStream, QDate, QReadWriteLock, QThread,QIODevice, Qt)
from PyQt5.QtWidgets import (QApplication, QMessageBox, QPushButton)
from PyQt5.QtNetwork import (QAbstractSocket,QHostAddress, QTcpServer, QTcpSocket)

PORT = 9407
SIZEOF_UINT16 = 2
MAX_BOOKINGS_PER_DAY = 5

# Key = date, value = list of room IDs
Bookings = collections.defaultdict(list)


def printBookings():
  for key in sorted(Bookings):
    print(key, Bookings[key])
  print()


class Thread(QThread):

  lock = QReadWriteLock()

  def __init__(self, socketId, parent):
    super(Thread, self).__init__(parent)
    self.socketId = socketId


  def run(self):
    socket = QTcpSocket()
    if not socket.setSocketDescriptor(self.socketId):
      #self.emit(SIGNAL("error(int)"), socket.error())
      self.error.connect(socket.error)
      return
    while socket.state() == QAbstractSocket.ConnectedState:
      nextBlockSize = 0
      stream = QDataStream(socket)
      stream.setVersion(QDataStream.Qt_5_7)
      if (socket.waitForReadyRead() and
        socket.bytesAvailable() >= SIZEOF_UINT16):
        nextBlockSize = stream.readUInt16()
      else:
        self.sendError(socket, "Cannot read client request")
        return
      if socket.bytesAvailable() < nextBlockSize:
        if (not socket.waitForReadyRead(60000) or
          socket.bytesAvailable() < nextBlockSize):
          self.sendError(socket, "Cannot read client data")
          return
      action = ""
      room = ""
      date = QDate()
      action=stream.readQString()
      if action in ("BOOK", "UNBOOK"):
        room=stream.readQString()
        stream >> date
        try:
          Thread.lock.lockForRead()
          bookings = Bookings.get(date.toPyDate())
        finally:
          Thread.lock.unlock()
        uroom = str(room)
      if action == "BOOK":
        newlist = False
        try:
          Thread.lock.lockForRead()
          if bookings is None:
            newlist = True
        finally:
          Thread.lock.unlock()
        if newlist:
          try:
            Thread.lock.lockForWrite()
            bookings = Bookings[date.toPyDate()]
          finally:
            Thread.lock.unlock()
        error = None
        insert = False
        try:
          Thread.lock.lockForRead()
          if len(bookings) < MAX_BOOKINGS_PER_DAY:
            if uroom in bookings:
              error = "Cannot accept duplicate booking"
            else:
              insert = True
          else:
            error = "{0} is fully booked".format(date.toString(Qt.ISODate))
        finally:
          Thread.lock.unlock()
        if insert:
          try:
            Thread.lock.lockForWrite()
            bisect.insort(bookings, uroom)
          finally:
            Thread.lock.unlock()
          self.sendReply(socket, action, room, date)
        else:
          self.sendError(socket, error)
      elif action == "UNBOOK":
        error = None
        remove = False
        try:
          Thread.lock.lockForRead()
          if bookings is None or uroom not in bookings:
            error = "Cannot unbook nonexistent booking"
          else:
            remove = True
        finally:
          Thread.lock.unlock()
        if remove:
          try:
            Thread.lock.lockForWrite()
            bookings.remove(uroom)
          finally:
            Thread.lock.unlock()
          self.sendReply(socket, action, room, date)
        else:
          self.sendError(socket, error)
      else:
        self.sendError(socket, "Unrecognized request")
      socket.waitForDisconnected()
      try:
        Thread.lock.lockForRead()
        printBookings()
      finally:
        Thread.lock.unlock()


  def sendError(self, socket, msg):
    reply = QByteArray()
    stream = QDataStream(reply, QIODevice.WriteOnly)
    stream.setVersion(QDataStream.Qt_5_7)
    stream.writeUInt16(0)
    stream.writeQString("ERROR")
    stream.writeQString(msg)
    stream.device().seek(0)
    stream.writeUInt16(reply.size() - SIZEOF_UINT16)
    socket.write(reply)

  def sendReply(self, socket, action, room, date):
    reply = QByteArray()
    stream = QDataStream(reply, QIODevice.WriteOnly)
    stream.setVersion(QDataStream.Qt_5_7)
    stream.writeUInt16(0)
    stream.writeQString(action)
    stream.writeQString(room)
    stream<<date
    stream.device().seek(0)
    stream.writeUInt16(reply.size() - SIZEOF_UINT16)
    socket.write(reply)

class TcpServer(QTcpServer):

  def __init__(self, parent=None):
    super(TcpServer, self).__init__(parent)


  def incomingConnection(self, socketId):
    thread = Thread(socketId, self)
    #self.connect(thread, SIGNAL("finished()"),
    #       thread, SLOT("deleteLater()"))
    thread.finished.connect(thread.deleteLater)
    thread.start()


class BuildingServicesDlg(QPushButton):

  def __init__(self, parent=None):
    super(BuildingServicesDlg, self).__init__(
        "&Close Server", parent)
    self.setWindowFlags(Qt.WindowStaysOnTopHint)

    self.loadBookings()
    self.tcpServer = TcpServer(self)
    if not self.tcpServer.listen(QHostAddress("0.0.0.0"), PORT):
      QMessageBox.critical(self, "Building Services Server","Failed to start server: {0}".format(self.tcpServer.errorString()))
      self.close()
      return

    self.clicked.connect(self.close)
    font = self.font()
    font.setPointSize(24)
    self.setFont(font)
    self.setWindowTitle("Building Services Server")


  def loadBookings(self):
    # Generate fake data
    import random

    today = QDate.currentDate()
    for i in range(10):
      date = today.addDays(random.randint(7, 60))
      for j in range(random.randint(1, MAX_BOOKINGS_PER_DAY)):
        # Rooms are 001..534 excl. 100, 200, ..., 500
        floor = random.randint(0, 5)
        room = random.randint(1, 34)
        bookings = Bookings[date.toPyDate()]
        if len(bookings) >= MAX_BOOKINGS_PER_DAY:
          continue
        bisect.insort(bookings, "{0:1d}{1:02d}".format(
               floor, room))
    printBookings()


app = QApplication(sys.argv)
form = BuildingServicesDlg()
form.show()
form.move(0, 0)
app.exec_()

以上这篇python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python根据文件大小打log日志
Oct 09 Python
Python实现SSH远程登陆,并执行命令的方法(分享)
May 08 Python
python使用super()出现错误解决办法
Aug 14 Python
python用fsolve、leastsq对非线性方程组求解
Dec 15 Python
Python3使用Matplotlib 绘制精美的数学函数图形
Apr 11 Python
Django中提示消息messages的设置方式
Nov 15 Python
python实现单目标、多目标、多尺度、自定义特征的KCF跟踪算法(实例代码)
Jan 08 Python
Python 日期的转换及计算的具体使用详解
Jan 16 Python
python 安装库几种方法之cmd,anaconda,pycharm详解
Apr 08 Python
自定义Django_rest_framework_jwt登陆错误返回的解决
Oct 18 Python
python 如何上传包到pypi
Dec 24 Python
Python激活Anaconda环境变量的详细步骤
Jun 08 Python
python 应用之Pycharm 新建模板默认添加编码格式-作者-时间等信息【推荐】
Jun 17 #Python
python3+PyQt5 使用三种不同的简便项窗口部件显示数据的方法
Jun 17 #Python
对PyQt5中树结构的实现方法详解
Jun 17 #Python
PyQT实现菜单中的复制,全选和清空的功能的方法
Jun 17 #Python
使用python接入微信聊天机器人
Mar 31 #Python
基于树莓派的语音对话机器人
Jun 17 #Python
PyQt5 QListWidget选择多项并返回的实例
Jun 17 #Python
You might like
ecshop 订单确认中显示省市地址信息的方法
2010/03/15 PHP
PHP生成腾讯云COS接口需要的请求签名
2018/05/20 PHP
PHP asXML()函数讲解
2019/02/03 PHP
javascript Zifa FormValid 0.1表单验证 代码打包下载
2007/06/08 Javascript
JS 面向对象的5钟写法
2009/07/31 Javascript
ext combox 下拉框不出现自动提示,自动选中的解决方法
2010/02/24 Javascript
sencha touch 模仿tabpanel导航栏TabBar的实例代码
2013/10/24 Javascript
jquery动态加载select下拉框示例代码
2013/12/10 Javascript
JQuery中clone方法复制节点
2015/05/18 Javascript
JavaScript模拟push
2016/03/06 Javascript
js输出数据精确到小数点后n位代码
2016/07/02 Javascript
解决npm安装Electron缓慢网络超时导致失败的问题
2018/02/06 Javascript
Vue中的transition封装组件的实现方法
2019/08/13 Javascript
node.js 使用 net 模块模拟 websocket 握手进行数据传递操作示例
2020/02/11 Javascript
基于JavaScript的数据结构队列动画实现示例解析
2020/08/06 Javascript
vue 封装面包屑组件教程
2020/11/16 Javascript
[57:38]2018DOTA2亚洲邀请赛3月30日 小组赛A组 OpTic VS OG
2018/03/31 DOTA
python使用marshal模块序列化实例
2014/09/25 Python
python中os操作文件及文件路径实例汇总
2015/01/15 Python
Python时间戳使用和相互转换详解
2017/12/11 Python
Python爬虫实现(伪)球迷速成
2018/06/10 Python
如何不用安装python就能在.NET里调用Python库
2019/07/12 Python
pytorch中的embedding词向量的使用方法
2019/08/18 Python
Python用来做Web开发的优势有哪些
2020/08/05 Python
java字符串格式化输出实例讲解
2021/01/06 Python
CSS3中的Media Queries学习笔记
2016/05/23 HTML / CSS
意大利男装网店:Vrients
2019/05/02 全球购物
Carmen Sol官网:购买果冻鞋、手袋和配件
2021/01/01 全球购物
应届毕业生求职信范文分享
2013/12/26 职场文书
学术会议主持词
2014/03/17 职场文书
竞争上岗实施方案
2014/03/21 职场文书
幼儿园的门卫岗位职责
2014/04/10 职场文书
经贸日语专业个人求职信范文
2014/04/29 职场文书
Python绘制分类图的方法
2021/04/20 Python
CSS3新特性详解(五):多列columns column-count和flex布局
2021/04/30 HTML / CSS
Vue接口封装的完整步骤记录
2021/05/14 Vue.js