import time
import socket
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from threading import Thread
class Chat(QWidget):
def __init__(self):
QWidget. __init__ (self)
self.setWindowTitle('PyChat')
self.resize(1200,800)
self.setMaximumSize(QSize(1200, 800))
self.setMinimumSize(QSize(600, 400))
self.chatfield = QPlainTextEdit(self)
self.chatfield.setGeometry(QRect(25, 25, 900, 705))
self.connectme = QPushButton(self)
self.connectme.setGeometry(QRect(950, 150, 140, 40))
self.connectme.setText('Соединение')
self.userName = QLineEdit(self)
self.userName.setGeometry(QRect(950, 100, 175, 25))
self.enterNamePls = QLabel(self)
self.enterNamePls.setText('Введите ваше имя:')
self.enterNamePls.setGeometry(QRect(950, 50, 100, 25))
self.writehere = QLineEdit(self)
self.writehere.setGeometry(QRect(25, 740, 900, 35))
self.reply = QPushButton(self)
self.reply.setGeometry(QRect(940, 740, 65, 35))
self.reply.setText('Ответ')
self.reply.clicked.connect(self.my_broadcast)
self.listen_Th = Thread(target = self.my_listen)
self.connectme.clicked.connect(lambda event='<Button-1>' : self.listen_Th.start())
self.broadsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.broadsock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
self.nick = 'Anonymous'
self.show()
def my_listen(self):
self.nick = self.userName.text()
mysock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
mysock.bind(('',10032))
self.connectme.setDisabled(True)
while True:
message = mysock.recv(256)
self.chatfield.insertPlainText('['+time.strftime('%X')+ '] '+ message.decode()+'\n')
def my_broadcast(self):
try:
myreply = self.writehere.text()
self.broadsock.sendto((self.nick+': ' + myreply).encode(), ('255.255.255.255', 10032))
self.writehere.clear()
except:
pass
if __name__ == '__main__':
import sys
myapp = QApplication(sys.argv)
mywin = Chat()
sys.exit(myapp.exec_())
aW1wb3J0IHRpbWUKaW1wb3J0IHNvY2tldApmcm9tIFB5UXQ1LlF0V2lkZ2V0cyBpbXBvcnQgKgpmcm9tIF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ICAgIHNlbGYuY29ubmVjdG1lLnNldERpc2FibGVkKFRydWUpCiAgICAgICAgd2hpbGUgVHJ1ZToKICAgICAgICAgICAgbWVzc2FnZSA9IG15c29jay5yZWN2KDI1NikKICAgICAgICAgICAgc2VsZi5jaGF0ZmllbGQuaW5zZXJ0UGxhaW5UZXh0KCdbJyt0aW1lLnN0cmZ0aW1lKCclWCcpKyAnXSAnKyBtZXNzYWdlLmRlY29kZSgpKydcbicpCgogICAgZGVmIG15X2Jyb2FkY2FzdChzZWxmKToKICAgICAgICB0cnk6CiAgICAgICAgICAgIG15cmVwbHkgPSBzZWxmLndyaXRlaGVyZS50ZXh0KCkKICAgICAgICAgICAgc2VsZi5icm9hZHNvY2suc2VuZHRvKChzZWxmLm5pY2srJzogJyArIG15cmVwbHkpLmVuY29kZSgpLCAoJzI1NS4yNTUuMjU1LjI1NScsIDEwMDMyKSkKICAgICAgICAgICAgc2VsZi53cml0ZWhlcmUuY2xlYXIoKQogICAgICAgIGV4Y2VwdDoKICAgICAgICAgICAgcGFzcwogICAgICAgIAppZiBfX25hbWVfXyA9PSAnX19tYWluX18nOgogICAgaW1wb3J0IHN5cwogICAgbXlhcHAgPSBRQXBwbGljYXRpb24oc3lzLmFyZ3YpCiAgICBteXdpbiA9IENoYXQoKQogICAgc3lzLmV4aXQobXlhcHAuZXhlY18oKSk=