Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. #!/usr/bin/python
  2. from __future__ import division
  3. import multiprocessing
  4. import sys, socket
  5. import threading
  6. from PyQt4 import QtCore, QtGui, uic
  7.  
  8.  
  9. qtCreatorFile = "mainwindow.ui"
  10.  
  11. Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
  12.  
  13. def handle(myApp, connection, address):
  14.     import logging
  15.     logging.basicConfig(level=logging.DEBUG)
  16.     logger = logging.getLogger("process-%r" % (address,))
  17.     try:
  18.         logger.debug("Connected %r at %r", connection, address)
  19.         while True:
  20.             data = connection.recv(1024)
  21.             if data == "":
  22.                 logger.debug("Socket closed remotely")
  23.                 break
  24.             logger.debug("Received data %r", data)
  25.             with open("output.log", "a") as myfile:
  26.                 myfile.write(data)
  27.             myApp.lblSideVal.setText(data)
  28.             #connection.sendall("Got It")
  29.             logger.debug("Sent data")
  30.     except:
  31.         logger.exception("Problem handling request")
  32.     finally:
  33.         logger.debug("Closing socket")
  34.         connection.close()
  35.  
  36. class Server(object):
  37.     def __init__(myApp, hostname, port):
  38.         import logging
  39.         self.logger = logging.getLogger("server")
  40.         self.myApp = myApp
  41.         self.hostname = hostname
  42.         self.port = port
  43.  
  44.     def start(self):
  45.         self.logger.debug("listening")
  46.         self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  47.         self.socket.bind((self.hostname, self.port))
  48.         self.socket.listen(1)
  49.  
  50.         while True:
  51.             conn, address = self.socket.accept()
  52.             self.logger.debug("Got connection")
  53.             process = multiprocessing.Process(target=handle, args=(self.myApp, conn, address))
  54.             process.daemon = True
  55.             process.start()
  56.             self.logger.debug("Started process %r", process)
  57.  
  58. class MyApp(QtGui.QMainWindow, Ui_MainWindow):
  59.     def __init__(self):
  60.         QtGui.QMainWindow.__init__(self)
  61.         Ui_MainWindow.__init__(self)
  62.         self.setupUi(self)
  63.         #self.calc_tax_button.clicked.connect(self.CalculateTax)
  64.     self.lblMixVal.setText("3770835")
  65.     self.lblSideVal.setText("LA")
  66.     self.lblPLCVal.setText("--D-RRS")
  67.         self.server = Server(self, '0.0.0.0',6666)
  68.         server_thread=threading.Thread(target=self.server.start)
  69.         server_thread.start()
  70.  
  71.  
  72.  
  73. if __name__ == "__main__":
  74.     app = QtGui.QApplication(sys.argv)
  75.     import logging
  76.     logging.basicConfig(level=logging.DEBUG)
  77.     window = MyApp()
  78.     window.show()
  79.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement