import sys from qt import * from qtnetwork import * true=1 false=0 class ICSMainWindow(QMainWindow): def __init__(self, *args): apply(QMainWindow.__init__, (self, ) + args) self.setGeometry(10,10,600,600) self.ICShost=QLineEdit("freechess.org", self) self.ICShost.setGeometry(10,10,470,20) self.ICSconnectButton=QPushButton("Connect", self) self.ICSconnectButton.setGeometry(490,10,100,20) self.connect( self.ICSconnectButton, SIGNAL("clicked()"), self.connectToICS) self.ICSconsole=QTextEdit(self) self.ICSconsole.setReadOnly(true) self.ICSconsole.setGeometry(10,50,580,510) self.ICSinput=QLineEdit("",self) self.ICSinput.setGeometry(10,570,470,20) self.ICSsendButton=QPushButton("Send", self) self.ICSsendButton.setGeometry(490,570,100,20) self.connect( self.ICSsendButton, SIGNAL("clicked()"), self.ICSsendToServer) self.ICSsocket=QSocket(self) self.connect( self.ICSsocket, SIGNAL("connected()"),self.socketConnected) self.connect( self.ICSsocket, SIGNAL("connectionClosed()"),self.socketConnectionClosed) self.connect( self.ICSsocket, SIGNAL("readyRead()"),self.socketReadyRead) self.connect( self.ICSsocket, SIGNAL("error(int)"),self.socketError) def connectToICS(self): self.ICSconsole.append("Trying to connect to the server\n") self.ICSsocket.connectToHost( "freechess.org", 5000 ); def closeConnection(self): self.ICSsocket.close(); def socketReadyRead(self): while self.ICSsocket.canReadLine(): self.ICSconsole.append( self.ICSsocket.readLine()) def socketConnected(self): self.ICSconsole.append("Connected to server\n") def socketConnectionClosed(self): ICSconsole.append("Connection closed by the server\n") def socketClosed(self): ICSconsole.append("Connection closed\n") def socketError( self): ICSconsole.append("Error occurred\n"); def ICSconnectToHost(host,port): self.ICSconsole.append("Trying to connect to the server\n"); self.ICSsocket.connectToHost( self.ICShost.text(), 5000 ); def ICSsendToServer(self): os=QTextStream(self.ICSsocket) os.write("%s\n" % self.ICSinput.text()) self.ICSinput.setText( "" ) def main(args): app=QApplication(args) win=ICSMainWindow() win.show() app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()")) app.exec_loop() if __name__=="__main__": main(sys.argv)