Advertisement
Guest User

PyQt4 QTcpSocket test

a guest
Jul 22nd, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import sys
  2. from PyQt4 import QtCore,QtGui,QtNetwork
  3.  
  4. class test(QtGui.QWidget):
  5.   def __init__(self,parent=None):
  6.     QtGui.QWidget.__init__(self,parent)
  7.     self.layout=QtGui.QVBoxLayout(self)
  8.     self.toSent=QtGui.QLineEdit(self)
  9.     self.received=QtGui.QLineEdit(self)
  10.     self.button=QtGui.QPushButton('SEND')
  11.     self.layout.addWidget(self.toSent)
  12.     self.layout.addWidget(self.received)
  13.     self.layout.addWidget(self.button)
  14.    
  15.     self.soc=QtNetwork.QTcpSocket(self)
  16.    
  17.     self.button.clicked.connect(self.connecting)
  18.     self.soc.connected.connect(self.sending)
  19.     self.soc.bytesWritten.connect(self.reading)
  20.    
  21.    
  22.     self.show()
  23.    
  24.   def connecting(self):
  25.     self.soc.connectToHost('192.168.1.74',23000)
  26.  
  27.   def sending(self):
  28.     payload=self.toSent.text()
  29.     payload.append(chr(13))
  30.     print self.soc.writeData(payload)
  31.    
  32.   def reading(self):
  33.    
  34.    
  35.     a=str(self.soc.readAll())
  36.     print 'READ:',a
  37.     self.received.setText(a)
  38.     self.soc.disconnectFromHost()
  39.    
  40.    
  41.  
  42. app=QtGui.QApplication(sys.argv)
  43. ins=test()
  44. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement