Advertisement
toko214

client

Jun 14th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 KB | None | 0 0
  1. import threading
  2. import cPickle
  3. import chat #ui
  4. import login #ui
  5. import sys
  6. from sys import exit as kick
  7. from PyQt4 import  QtGui, QtCore
  8. import ssl
  9. import socket
  10. import io
  11. from PIL import Image
  12. import numpy.core.multiarray
  13. import cv2
  14.  
  15.  
  16. HOST,PORT=('79.178.113.229',8200)
  17. con_socket = None
  18. my_sok = None
  19. server_private_key = None
  20. cursor = None
  21. SEND_MSG = "You Wrote: "
  22. UTF8 = 'utf8'
  23. ACCEPTED = 'in'
  24. DETAILS = "Please Fill All The Details"
  25. JOINED_MSG = "You Have Joined The Server!"
  26. mainForm = None
  27. cap = None
  28.  
  29. class ChatUI(QtGui.QMainWindow, chat.Ui_MainWindow):
  30.     def __init__(self):
  31.         # Explaining super is out of the scope of this article
  32.         # So please google it if you're not familar with it
  33.         # Simple reason why we use it here is that it allows us to
  34.         # access variables, methods etc in the design.py file
  35.         super(self.__class__, self).__init__()
  36.         self.setupUi(self)  # This is defined in design.py file automatically
  37.         # It sets up layout and widgets that are defined
  38.         self.pushButton.clicked.connect(self.sendMSG)
  39.         t = threading.Thread(target=self.recv_msg)
  40.         t.daemon = True
  41.         t.start()
  42.         t = threading.Thread(target=video_client)
  43.         t.daemon = True
  44.         t.start()
  45.  
  46.  
  47.     def sendMSG(self):
  48.         self.textBrowser.append(SEND_MSG+self.plainTextEdit.toPlainText())
  49.         con_socket.write(str(self.plainTextEdit.toPlainText()))
  50.  
  51.     def recv_msg(self):
  52.         self.textBrowser.append(JOINED_MSG)
  53.         while True:
  54.             try:
  55.                 msg = con_socket.read()
  56.                 print(msg)
  57.                 self.textBrowser.append(msg.decode(UTF8))
  58.             except Exception as ex:
  59.                 print(ex)
  60.                 break
  61.  
  62.     def closeEvent(self, hey):
  63.         # Your desired functionality here
  64.         try:
  65.             con_socket.close()
  66.             my_sok.close()
  67.             print "closing"
  68.         except:
  69.             pass
  70.         cap.release()
  71.         cv2.destroyAllWindows()
  72.         QtCore.QCoreApplication.quit()
  73.         kick(0)
  74.      
  75. class LoginUI(QtGui.QDialog):
  76.     def __init__(self,parent = None):
  77.         QtGui.QDialog.__init__(self, parent)
  78.         self.ui = login.Ui_Dialog()
  79.         self.ui.setupUi(self)
  80.         self.ui.pushButton.clicked.connect(self.loadMainUi)
  81.  
  82.     def loadMainUi(self):
  83.         global con_socket
  84.         print len(self.ui.lineEdit.text())
  85.         if len(self.ui.lineEdit.text()) > 0 and len(self.ui.lineEdit_2.text())>0:
  86.             con_socket = socket.socket()
  87.             con_socket = ssl.wrap_socket(con_socket, ca_certs="5259214-gg.cert",cert_reqs=ssl.CERT_REQUIRED)
  88.             con_socket.connect((HOST, PORT))
  89.             to_send = cPickle.dumps([self.ui.lineEdit.text(),self.ui.lineEdit_2.text()])
  90.             con_socket.write(to_send)
  91.             state = con_socket.read()
  92.             if state == ACCEPTED:
  93.                 self.accept()
  94.             else:
  95.                 print state
  96.                 con_socket.close()
  97.                 con_socket = None
  98.         else:
  99.             print DETAILS
  100.  
  101.     def closeEvent(self, hey):
  102.         # Your desired functionality here
  103.         kick(0)
  104.  
  105.  
  106. def video_client():
  107.     global my_sok
  108.     global cap
  109.     cap = cv2.VideoCapture(0)
  110.     my_sok = socket.socket()
  111.     my_sok.connect((HOST,44000))
  112.     while True:
  113.         try:
  114.             ret, frame2 = cap.read()
  115.             img = cv2.cvtColor(frame2, cv2.COLOR_BGR2BGRA)
  116.             cv2.imshow("frame", img)
  117.             pil_im = Image.fromarray(img)
  118.             b = io.BytesIO()
  119.             pil_im.save(b, 'jpeg')
  120.             im_bytes = b.getvalue()
  121.             my_sok.send(im_bytes)
  122.             cv2.waitKey(1)
  123.         except socket.error as ex:
  124.             print "I have catch a socket.error\n\n%s"%ex
  125.             my_sok.close()
  126.             break
  127.         except Exception as ez:
  128.             print "I have catch a Exception\n\n%s"%ez
  129.             break
  130.     cap.release()
  131.     cv2.destroyAllWindows()
  132.  
  133. def main():
  134.     global mainForm
  135.     app = QtGui.QApplication(sys.argv)  # A new instance of QApplication
  136.     loginForm = LoginUI()
  137.     state = loginForm.exec_()
  138.     while state != QtGui.QDialog.Accepted:
  139.         state = loginForm.exec_()
  140.     mainForm = ChatUI()
  141.     mainForm.show()
  142.     app.exec_()
  143.     print "Closing"
  144.  
  145.  
  146. if __name__ == '__main__':
  147.     try:
  148.         main()
  149.     except:
  150.         con_socket.close()
  151.         con_socket = None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement