یه چیزی مثل این باید کارت رو راه بندازه:
سرور
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
import os
class Echo(Protocol):
def connectionMade(self):
kernel = os.popen('uname -r').readline()
self.transport.write(kernel)
def dataReceived(self, data):
print data
def main():
f = Factory()
f.protocol = Echo
reactor.listenTCP(8000, f)
reactor.run()
if __name__ == '__main__':
main()
کلاینت
from twisted.internet.protocol import ClientFactory
from twisted.internet import reactor, protocol
import sys
class EchoClient(protocol.Protocol):
def connectionMade(self):
pass
def dataReceived(self, data):
print "receive:", data
a = raw_input('type something:')
self.transport.write(a)
class EchoClientFactory(ClientFactory):
protocol = EchoClient
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
def main():
factory = EchoClientFactory()
reactor.connectTCP('localhost', 8000, factory)
reactor.run()
if __name__ == '__main__':
main()