Skip to content

Instantly share code, notes, and snippets.

@umitanuki
Created May 14, 2010 15:36
Show Gist options
  • Save umitanuki/401302 to your computer and use it in GitHub Desktop.
Save umitanuki/401302 to your computer and use it in GitHub Desktop.
from twisted.internet import reactor
from twisted.internet.protocol import Factory, Protocol, ClientFactory
import sys
HOST = 'servername'
class ClientProtocol(Protocol):
def dataReceived(self, data):
self.server_protocol.transport.write(data)
class MyClientFactory(ClientFactory):
def __init__(self, server_protocol):
self.server_protocol = server_protocol
def buildProtocol(self, addr):
print 'Connected.'
client_protocol = ClientProtocol()
client_protocol.server_protocol = self.server_protocol
self.server_protocol.client_protocol = client_protocol
return client_protocol
def clientConnectionLost(self, connector, reason):
print 'Lost connection. Reason:', reason
def clientConnectionFailed(self, connector, reason):
print 'Connection failed. Reason:', reason
class ServerProtocol(Protocol):
def connectionMade(self):
self.client_protocol = None
reactor.connectTCP(HOST, 22, MyClientFactory(self))
def dataReceived(self, data):
self.client_protocol.transport.write(data)
def connectionLost(self, reason):
print 'Server connection lost. Reason:', reason
self.client_protocol.transport.loseConnection()
class ServerFactory(Factory):
protocol = ServerProtocol
def main():
# cfact= MyClientFactory()
sfact = ServerFactory()
# reactor.connectTCP('pooh', 22, cfact)
reactor.listenTCP(2222, sfact)
reactor.run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment