Guest User

P2P Null-Byte Chat App Python

a guest
Aug 4th, 2015
3,677
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2.  
  3. # Form implementation generated from reading ui file 'chat_client1.ui'
  4. #
  5. # Created: Thu Jul 30 16:47:54 2015
  6. # by: PyQt4 UI code generator 4.9.3
  7. #
  8. # WARNING! All changes made in this file will be lost!
  9.  
  10. from PyQt4.QtCore import *
  11. from PyQt4.QtGui import *
  12. import sys, socket
  13. from thread import *
  14.  
  15. def app_version():
  16. msg_box("Application Version", "Null-Byte P2P Chat v1.0")
  17.  
  18. def msg_box(title, data):
  19. w = QWidget()
  20. QMessageBox.information(w, title, data)
  21.  
  22. def update_list(self, data):
  23. self.listWidget.addItem(data)
  24. print "\a"
  25.  
  26. def server_socket(self):
  27. try:
  28. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29. s.bind(('', 6190))
  30. s.listen(1)
  31. except socket.error, e:
  32. msg_box("Socket Error !!",
  33. "Unable To Setup Local Socket. Port In Use")
  34. return
  35.  
  36. while 1:
  37. conn, addr = s.accept()
  38.  
  39. incoming_ip = str(addr[0])
  40. current_chat_ip = self.lineEdit.text()
  41.  
  42. if incoming_ip != current_chat_ip:
  43. conn.close()
  44. else:
  45. data = conn.recv(4096)
  46. update_list(self, data)
  47. conn.close()
  48.  
  49. s.close()
  50.  
  51. try:
  52. _fromUtf8 = QString.fromUtf8
  53. except AttributeError:
  54. _fromUtf8 = lambda s: s
  55.  
  56. class Ui_MainWindow(object):
  57. def setupUi(self, MainWindow):
  58.  
  59. self.start_server()
  60.  
  61. MainWindow.setObjectName(_fromUtf8("MainWindow"))
  62. MainWindow.resize(662, 448)
  63. self.centralwidget = QWidget(MainWindow)
  64. self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
  65. self.frame = QFrame(self.centralwidget)
  66. self.frame.setGeometry(QRect(10, 10, 651, 41))
  67. self.frame.setFrameShape(QFrame.StyledPanel)
  68. self.frame.setFrameShadow(QFrame.Raised)
  69. self.frame.setObjectName(_fromUtf8("frame"))
  70. self.label = QLabel(self.frame)
  71. self.label.setGeometry(QRect(10, 10, 131, 21))
  72. self.label.setObjectName(_fromUtf8("label"))
  73. self.lineEdit = QLineEdit(self.frame)
  74. self.lineEdit.setGeometry(QRect(90, 10, 161, 21))
  75. self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
  76. self.label_2 = QLabel(self.frame)
  77. self.label_2.setGeometry(QRect(260, 10, 131, 21))
  78. self.label_2.setObjectName(_fromUtf8("label_2"))
  79. self.lineEdit_2 = QLineEdit(self.frame)
  80. self.lineEdit_2.setGeometry(QRect(300, 10, 121, 21))
  81. self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
  82. self.frame_2 = QFrame(self.centralwidget)
  83. self.frame_2.setGeometry(QRect(10, 60, 301, 321))
  84. self.frame_2.setFrameShape(QFrame.StyledPanel)
  85. self.frame_2.setFrameShadow(QFrame.Raised)
  86. self.frame_2.setObjectName(_fromUtf8("frame_2"))
  87. self.textEdit = QTextEdit(self.frame_2)
  88. self.textEdit.setGeometry(QRect(10, 10, 281, 261))
  89. self.textEdit.setObjectName(_fromUtf8("textEdit"))
  90. self.pushButton_3 = QPushButton(self.frame_2)
  91. self.pushButton_3.setGeometry(QRect(10, 280, 171, 31))
  92. self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
  93.  
  94.  
  95. #############################################################
  96. # Executes When The Send Message Button Is Clicked
  97. self.pushButton_3.clicked.connect(self.client_send_message)
  98. ############################################################
  99.  
  100.  
  101. self.pushButton_4 = QPushButton(self.frame_2)
  102. self.pushButton_4.setGeometry(QRect(190, 280, 93, 31))
  103. self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
  104.  
  105.  
  106. #############################################################
  107. # Executes When The Clear Logs Button Is Clicked
  108. self.pushButton_4.clicked.connect(self.clear_logs)
  109. ##############################################################
  110.  
  111.  
  112. self.frame_3 = QFrame(self.centralwidget)
  113. self.frame_3.setGeometry(QRect(320, 60, 331, 321))
  114. self.frame_3.setFrameShape(QFrame.StyledPanel)
  115. self.frame_3.setFrameShadow(QFrame.Raised)
  116. self.frame_3.setObjectName(_fromUtf8("frame_3"))
  117. self.listWidget = QListWidget(self.frame_3)
  118. self.listWidget.setGeometry(QRect(10, 10, 311, 301))
  119. self.listWidget.setObjectName(_fromUtf8("listWidget"))
  120. MainWindow.setCentralWidget(self.centralwidget)
  121. self.statusbar = QStatusBar(MainWindow)
  122. self.statusbar.setObjectName(_fromUtf8("statusbar"))
  123. MainWindow.setStatusBar(self.statusbar)
  124. self.menubar = QMenuBar(MainWindow)
  125. self.menubar.setGeometry(QRect(0, 0, 662, 29))
  126. self.menubar.setObjectName(_fromUtf8("menubar"))
  127. self.menuAction = QMenu(self.menubar)
  128. self.menuAction.setObjectName(_fromUtf8("menuAction"))
  129. MainWindow.setMenuBar(self.menubar)
  130. self.actionExit = QAction(MainWindow)
  131. self.actionExit.setObjectName(_fromUtf8("actionExit"))
  132.  
  133. #######################################################
  134. # Executes When The SubMenu Item Version Is Clicked
  135. self.actionExit.triggered.connect(app_version)
  136. #######################################################
  137.  
  138. self.actionExit_2 = QAction(MainWindow)
  139. self.actionExit_2.setObjectName(_fromUtf8("actionExit_2"))
  140.  
  141. #######################################################
  142. # Executes When The SubMenu Item Exit Is Clicked
  143. self.actionExit_2.triggered.connect(qApp.quit)
  144. #######################################################
  145.  
  146. self.menuAction.addAction(self.actionExit)
  147. self.menuAction.addAction(self.actionExit_2)
  148. self.menubar.addAction(self.menuAction.menuAction())
  149.  
  150. self.retranslateUi(MainWindow)
  151. QMetaObject.connectSlotsByName(MainWindow)
  152.  
  153. def clear_logs(self):
  154. self.listWidget.clear()
  155.  
  156. def retranslateUi(self, MainWindow):
  157. MainWindow.setWindowTitle(QApplication.translate("MainWindow",
  158. "Null Byte P2P Chat", None, QApplication.UnicodeUTF8))
  159. self.label.setText(QApplication.translate("MainWindow", "IP Address:",
  160. None, QApplication.UnicodeUTF8))
  161. self.label_2.setText(QApplication.translate("MainWindow", "Nick: ",
  162. None, QApplication.UnicodeUTF8))
  163. self.pushButton_3.setText(QApplication.translate("MainWindow",
  164. "Send Message", None, QApplication.UnicodeUTF8))
  165. self.pushButton_4.setText(QApplication.translate("MainWindow",
  166. "Clear Logs", None, QApplication.UnicodeUTF8))
  167. self.menuAction.setTitle(QApplication.translate("MainWindow",
  168. "Menu Actions", None, QApplication.UnicodeUTF8))
  169. self.actionExit.setText(QApplication.translate("MainWindow",
  170. "Version", None, QApplication.UnicodeUTF8))
  171. self.actionExit_2.setText(QApplication.translate("MainWindow",
  172. "Exit", None, QApplication.UnicodeUTF8))
  173.  
  174. def start_server(self):
  175. start_new_thread(server_socket, (self,))
  176. msg_box("Success", "Server Started Sucessfully")
  177.  
  178. def client_send_message(self):
  179. ip_address = self.lineEdit.text()
  180.  
  181. nick = self.lineEdit_2.text()
  182. nick = nick.replace("#>","")
  183. rmessage = self.textEdit.toPlainText()
  184. rmessage = rmessage.replace("#>","")
  185.  
  186. rmsg = nick + " #> " + rmessage
  187.  
  188. c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  189.  
  190. try:
  191. c.connect((ip_address, 9000))
  192. except Exception, e:
  193. msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
  194. return
  195.  
  196. try:
  197. c.send(rmsg)
  198. self.listWidget.addItem(rmsg)
  199. self.textEdit.setText("")
  200. except Exception, e:
  201. msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
  202.  
  203. c.close()
  204.  
  205. if __name__ == "__main__":
  206. app = QApplication(sys.argv)
  207. MainWindow = QMainWindow()
  208. ui = Ui_MainWindow()
  209. ui.setupUi(MainWindow)
  210. MainWindow.show()
  211.  
  212. sys.exit(app.exec_())
RAW Paste Data