Demo entry 6645696

py

   

Submitted by lu on Oct 11, 2017 at 10:38
Language: Python 3. Code size: 3.4 kB.

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

import time, socket, threading, sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QDialog
from ui_box import Ui_Box
from ui_mainwindow import Ui_MainWindow

SIZE = 1024
HOST = '127.0.0.1'
PORT = 8880


class Client(object):
    def __init__(self):
        self.isClientConnect = False
        self.app = QtWidgets.QApplication(sys.argv)
        self.box = Ui_Box()
        self.box.loginbutton.clicked.connect(self.loginBtnClicked)
        self.mainWindow = Ui_MainWindow()
        self.mainWindow.sendButton.clicked.connect(self.sendBtnClicked)

    def sendBtnClicked(self):
        try:
            self.s.send(bytes(self.mainWindow.lineEdit.text(), 'utf-8'))
            self.mainWindow.lineEdit.setText('')
        except BaseException as ex:
            print(ex)
            QtWidgets.QMessageBox.critical(QtWidgets.QMessageBox(), "发送错误", "无法登陆到服务器,请检查账号密码以及网络连接")

    def loginBtnClicked(self):
        try:
            self.ConnectServer()
            time.sleep(0.5)
            recv = self.login_client()
        except BaseException as e:
            QtWidgets.QMessageBox.critical(QtWidgets.QMessageBox(), "错误", str(e))
            return

        if(self.isClientConnect):
            self.box.hide()
            self.ReceiveMessage()
        elif recv == 'exist':
            QtWidgets.QMessageBox.critical(QtWidgets.QMessageBox(), "登录错误", "用户已登录!")
        elif recv == 'notexist':
            QtWidgets.QMessageBox.critical(QtWidgets.QMessageBox(), "登录错误", "用户名不存在!")
        else:
            QtWidgets.QMessageBox.critical(QtWidgets.QMessageBox(), "登录错误", "密码错误!")



    def ConnectServer(self):
        print('- 正在连接服务器...')
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.connect((HOST, PORT))

    def login_client(self):
        print('- 正在登录中...')
        self.name = self.box.name.text()
        time.sleep(0.5)
        self.password = self.box.password.text()
        self.s.send(bytes(self.name, 'utf-8'))
        time.sleep(0.5)
        self.s.send(bytes(self.password, 'utf-8'))
        # 登录成功与否 信号的反馈
        receive = self.s.recv(SIZE).decode('utf-8')

        if receive == 'Welcome':
            print ('- 登录成功!')
            self.isClientConnect = True
        else:
            print ('- 登录失败!')

        return receive


    def ReceiveMessage(self):
        self.mainWindow.show()

        def ReceiveMessageHelp():
            model = QtGui.QStandardItemModel(self.mainWindow.listView)
            while True:
                try:
                    mesg = self.s.recv(SIZE).decode('utf-8')
                    curtime = time.strftime("%Y-%m-%d %H:%M:%S\t",time.localtime())
                    mesg = curtime + ' ' + mesg
                    tmp = QtGui.QStandardItem()
                    tmp.setText(mesg)
                    model.appendRow(tmp)
                    self.mainWindow.listView.setModel(model)
                    self.mainWindow.listView.update()
                except socket.error:
                    break
        subtrecv = threading.Thread(target=ReceiveMessageHelp, args=())
        subtrecv.setDaemon(True)
        subtrecv.start()



if __name__ == '__main__':
    a = Client()
    a.box.show()
    sys.exit(a.app.exec_())

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).