Advertisement
toko214

client

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