این فایل سرور :
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
import os
class Echo(Protocol):
def connectionMade(self):
kernel = os.popen('uname -a').readline()
self.transport.write(kernel)
def dataReceived(self, data, a):
if data == '1' :
print 'OHHHHHHHHHHHHHH :D'
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()
اروری که توی سرور میاد :
m4hdi@M4hdi:~/Desktop$ python server.py
Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 586, in _doReadOrWrite
why = selectable.doRead()
File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 199, in doRead
rval = self.protocol.dataReceived(data)
exceptions.TypeError: dataReceived() takes exactly 3 arguments (2 given)
اینم اروری که توی کلاینت میاد :
type something:1
connection lost: Connection was closed cleanly.