|
| 1 | +import sys,time |
| 2 | +from PyQt5 import QtGui |
| 3 | +from PyQt5 import QtCore |
| 4 | +from PyQt5.QtWidgets import QScrollBar,QSplitter,QTableWidgetItem,QTableWidget,QComboBox,QVBoxLayout,QGridLayout,QDialog,QWidget, QPushButton, QApplication, QMainWindow,QAction,QMessageBox,QLabel,QTextEdit,QProgressBar,QLineEdit |
| 5 | +from PyQt5.QtCore import QCoreApplication |
| 6 | +import socket |
| 7 | +from threading import Thread |
| 8 | +from socketserver import ThreadingMixIn |
| 9 | + |
| 10 | +conn=None |
| 11 | +class Window(QDialog): |
| 12 | + def __init__(self): |
| 13 | + super().__init__() |
| 14 | + self.flag=0 |
| 15 | + self.chatTextField=QLineEdit(self) |
| 16 | + self.chatTextField.resize(480,100) |
| 17 | + self.chatTextField.move(10,350) |
| 18 | + self.btnSend=QPushButton("Send",self) |
| 19 | + self.btnSend.resize(480,30) |
| 20 | + self.btnSendFont=self.btnSend.font() |
| 21 | + self.btnSendFont.setPointSize(15) |
| 22 | + self.btnSend.setFont(self.btnSendFont) |
| 23 | + self.btnSend.move(10,460) |
| 24 | + self.btnSend.setStyleSheet("background-color: #F7CE16") |
| 25 | + self.btnSend.clicked.connect(self.send) |
| 26 | + |
| 27 | + self.chatBody=QVBoxLayout(self) |
| 28 | + # self.chatBody.addWidget(self.chatTextField) |
| 29 | + # self.chatBody.addWidget(self.btnSend) |
| 30 | + # self.chatWidget.setLayout(self.chatBody) |
| 31 | + splitter=QSplitter(QtCore.Qt.Vertical) |
| 32 | + |
| 33 | + self.chat = QTextEdit() |
| 34 | + self.chat.setReadOnly(True) |
| 35 | + # self.chatLayout=QVBoxLayout() |
| 36 | + # self.scrollBar=QScrollBar(self.chat) |
| 37 | + # self.chat.setLayout(self.chatLayout) |
| 38 | + |
| 39 | + splitter.addWidget(self.chat) |
| 40 | + splitter.addWidget(self.chatTextField) |
| 41 | + splitter.setSizes([400,100]) |
| 42 | + |
| 43 | + splitter2=QSplitter(QtCore.Qt.Vertical) |
| 44 | + splitter2.addWidget(splitter) |
| 45 | + splitter2.addWidget(self.btnSend) |
| 46 | + splitter2.setSizes([200,10]) |
| 47 | + |
| 48 | + self.chatBody.addWidget(splitter2) |
| 49 | + |
| 50 | + |
| 51 | + self.setWindowTitle("Chat Application") |
| 52 | + self.resize(500, 500) |
| 53 | + |
| 54 | + def send(self): |
| 55 | + text=self.chatTextField.text() |
| 56 | + font=self.chat.font() |
| 57 | + font.setPointSize(13) |
| 58 | + self.chat.setFont(font) |
| 59 | + textFormatted='{:>80}'.format(text) |
| 60 | + self.chat.append(textFormatted) |
| 61 | + global conn |
| 62 | + conn.send(text.encode("utf-8")) |
| 63 | + self.chatTextField.setText("") |
| 64 | + |
| 65 | +class ServerThread(Thread): |
| 66 | + def __init__(self,window): |
| 67 | + Thread.__init__(self) |
| 68 | + self.window=window |
| 69 | + |
| 70 | + |
| 71 | + def run(self): |
| 72 | + TCP_IP = '0.0.0.0' |
| 73 | + TCP_PORT = 80 |
| 74 | + BUFFER_SIZE = 20 |
| 75 | + tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 76 | + tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 77 | + tcpServer.bind((TCP_IP, TCP_PORT)) |
| 78 | + threads = [] |
| 79 | + |
| 80 | + tcpServer.listen(4) |
| 81 | + while True: |
| 82 | + print("Multithreaded Python server : Waiting for connections from TCP clients...") |
| 83 | + global conn |
| 84 | + (conn, (ip,port)) = tcpServer.accept() |
| 85 | + newthread = ClientThread(ip,port,window) |
| 86 | + newthread.start() |
| 87 | + threads.append(newthread) |
| 88 | + |
| 89 | + |
| 90 | + for t in threads: |
| 91 | + t.join() |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +class ClientThread(Thread): |
| 96 | + |
| 97 | + def __init__(self,ip,port,window): |
| 98 | + Thread.__init__(self) |
| 99 | + self.window=window |
| 100 | + self.ip = ip |
| 101 | + self.port = port |
| 102 | + print("[+] New server socket thread started for " + ip + ":" + str(port)) |
| 103 | + |
| 104 | + def run(self): |
| 105 | + while True : |
| 106 | + |
| 107 | + #(conn, (self.ip,self.port)) = serverThread.tcpServer.accept() |
| 108 | + global conn |
| 109 | + data = conn.recv(2048) |
| 110 | + window.chat.append(data.decode("utf-8")) |
| 111 | + print(data) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == '__main__': |
| 115 | + app = QApplication(sys.argv) |
| 116 | + |
| 117 | + |
| 118 | + window = Window() |
| 119 | + serverThread=ServerThread(window) |
| 120 | + serverThread.start() |
| 121 | + window.exec() |
| 122 | + |
| 123 | + sys.exit(app.exec_()) |
0 commit comments