Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading
- import cPickle
- import chat #ui
- import login #ui
- import sys
- from sys import exit as kick
- from PyQt4 import QtGui, QtCore
- import ssl
- import socket
- import io
- from PIL import Image
- import numpy.core.multiarray
- import cv2
- HOST,PORT=('79.178.113.229',8200)
- con_socket = None
- my_sok = None
- server_private_key = None
- cursor = None
- SEND_MSG = "You Wrote: "
- UTF8 = 'utf8'
- ACCEPTED = 'in'
- DETAILS = "Please Fill All The Details"
- JOINED_MSG = "You Have Joined The Server!"
- mainForm = None
- cap = None
- class ChatUI(QtGui.QMainWindow, chat.Ui_MainWindow):
- def __init__(self):
- # Explaining super is out of the scope of this article
- # So please google it if you're not familar with it
- # Simple reason why we use it here is that it allows us to
- # access variables, methods etc in the design.py file
- super(self.__class__, self).__init__()
- self.setupUi(self) # This is defined in design.py file automatically
- # It sets up layout and widgets that are defined
- self.pushButton.clicked.connect(self.sendMSG)
- t = threading.Thread(target=self.recv_msg)
- t.daemon = True
- t.start()
- t = threading.Thread(target=video_client)
- t.daemon = True
- t.start()
- def sendMSG(self):
- self.textBrowser.append(SEND_MSG+self.plainTextEdit.toPlainText())
- con_socket.write(str(self.plainTextEdit.toPlainText()))
- def recv_msg(self):
- self.textBrowser.append(JOINED_MSG)
- while True:
- try:
- msg = con_socket.read()
- print(msg)
- self.textBrowser.append(msg.decode(UTF8))
- except Exception as ex:
- print(ex)
- break
- def closeEvent(self, hey):
- # Your desired functionality here
- try:
- con_socket.close()
- my_sok.close()
- print "closing"
- except:
- pass
- cap.release()
- cv2.destroyAllWindows()
- QtCore.QCoreApplication.quit()
- kick(0)
- class LoginUI(QtGui.QDialog):
- def __init__(self,parent = None):
- QtGui.QDialog.__init__(self, parent)
- self.ui = login.Ui_Dialog()
- self.ui.setupUi(self)
- self.ui.pushButton.clicked.connect(self.loadMainUi)
- def loadMainUi(self):
- global con_socket
- print len(self.ui.lineEdit.text())
- if len(self.ui.lineEdit.text()) > 0 and len(self.ui.lineEdit_2.text())>0:
- con_socket = socket.socket()
- con_socket = ssl.wrap_socket(con_socket, ca_certs="5259214-gg.cert",cert_reqs=ssl.CERT_REQUIRED)
- con_socket.connect((HOST, PORT))
- to_send = cPickle.dumps([self.ui.lineEdit.text(),self.ui.lineEdit_2.text()])
- con_socket.write(to_send)
- state = con_socket.read()
- if state == ACCEPTED:
- self.accept()
- else:
- print state
- con_socket.close()
- con_socket = None
- else:
- print DETAILS
- def closeEvent(self, hey):
- # Your desired functionality here
- kick(0)
- def video_client():
- global my_sok
- global cap
- cap = cv2.VideoCapture(0)
- my_sok = socket.socket()
- my_sok.connect((HOST,44000))
- while True:
- try:
- ret, frame2 = cap.read()
- img = cv2.cvtColor(frame2, cv2.COLOR_BGR2BGRA)
- cv2.imshow("frame", img)
- pil_im = Image.fromarray(img)
- b = io.BytesIO()
- pil_im.save(b, 'jpeg')
- im_bytes = b.getvalue()
- my_sok.send(im_bytes)
- cv2.waitKey(1)
- except socket.error as ex:
- print "I have catch a socket.error\n\n%s"%ex
- my_sok.close()
- break
- except Exception as ez:
- print "I have catch a Exception\n\n%s"%ez
- break
- cap.release()
- cv2.destroyAllWindows()
- def main():
- global mainForm
- app = QtGui.QApplication(sys.argv) # A new instance of QApplication
- loginForm = LoginUI()
- state = loginForm.exec_()
- while state != QtGui.QDialog.Accepted:
- state = loginForm.exec_()
- mainForm = ChatUI()
- mainForm.show()
- app.exec_()
- print "Closing"
- if __name__ == '__main__':
- try:
- main()
- except:
- con_socket.close()
- con_socket = None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement