1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #!/usr/bin/env python
# -*- coding: iso8859-1 -*-
import struct, wx, threading
#import wx.lib.newevent
from twisted.internet import reactor, defer
from twisted.internet.protocol import ClientFactory, Protocol, ReconnectingClientFactory
import socket
class TCPClientFactory(ReconnectingClientFactory):
def __init__(self):
self.protocol = ClientTCP
def startedConnecting(self, connector):
print "Started to connect."
def buildProtocol(self, addr):
print "Connected"
self.resetDelay()
return ClientTCP()
def clientConnectionLost(self, connector, reason):
print "Lost connection. Reason: %r" %(reason)
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def clientConnectionFailed(self, connector, reason):
print "Connection failed. Reason: %r" %(reason)
ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
class ClientTCP(Protocol):
def __init__(self):
self.buffer = None
def connectionMade(self):
self.buffer = ""
print 'Connected to host'
self.ReceivedCnx(self)
#print "ConnectionMade"
def connectionLost(self, reason):
self.buffer = None
#self.ReceivedDcnx(self.infoCnx)
print "ConnectionLost"
def ReceivedData(self,event=None):
print "ReceivedData",event
def ReceivedCnx(self,event=None):
print "ReceivedCnx",event
self.writeData("""<bus><field type='A' name='essai'>coucou</field></bus>""")
def ReceivedDcnx(self,event=None):
print "ReceivedDcnx",event
def writeData(self, data):
print "donnees envoyees : %r\n" %(data)
self.transport.write(data)
class ClientQuote:
def __init__(self):
self.__cnx = None
def connect(self, host, port):
#cree une socket TCP vers l adresse passee en parametre.
self.__cnx = TCPClientFactory()
d = defer.Deferred()
self.connector=reactor.connectTCP(host,port, self.__cnx)
reactor.callLater(1,d.callback,{'Hello':'hello'})
return d
def disconnect(self):
#deconnecte la socket TCP
self.__cnx.stop()
def send(self, dic):
print dic
self.__cnx.writeData(dic)
clQuot=ClientQuote()
d=clQuot.connect("localhost",12345)
d.addCallback(clQuot.send)
reactor.run() |
Partager