[Python-Twisted] Twisted入门之端口转发服务器 .
Twisted 是Python界很有名的一个基于异步事件的网络I/O框架,性能棒棒的,经历过BT的考验。本人垂涎很久了,于是写了一个端口转发服务器,纯练手~~~
需求:将windows的远程桌面做一个端口转发。
即:有三台机器分别为A B C.在C上打开远程桌面服务,开启3389端口。
在B上运行端口转发程序,将发往B的1099端口的数据发送到C的3389.
这样在A上通过远程桌面客户端访问B的1099端口就可以远程访问C的机器。
Understand?
let's go !!
Code :
from twisted.internet.protocol import Protocol,ClientCreator
from twisted.internet import reactor
from twisted.protocols.basic import LineReceiver
from twisted.internet.protocol import Factory,ClientFactory
class Transfer(Protocol):
def __init__(self):
pass
def connectionMade(self):
c = ClientCreator(reactor,Clienttransfer)
c.connectTCP("10.61.1.243",3389).addCallback(self.set_protocol)
self.transport.pauseProducing()
def set_protocol(self,p):
self.server = p
p.set_protocol(self)
def dataReceived(self,data):
self.server.transport.write(data)
def connectionLost(self,reason):
self.transport.loseConnection()
self.server.transport.loseConnection()
class Clienttransfer(Protocol):
def __init__(self):
pass
def set_protocol(self,p):
self.server = p
self.server.transport.resumeProducing()
pass
def dataReceived(self,data):
self.server.transport.write(data)
pass
factory = Factory()
factory.protocol = Transfer
reactor.listenTCP(1099,factory)
reactor.run()
写得比较仓促红色代码为转发的目的地址以及端口。
小测试了下。呵呵还行 远程桌面使用正常。我这个的实现比较恶,毫无构架扩展性而言,慢慢来 呵呵。
这个程序挺适合入门练手的,包括了服务端和客户端的写法,其中注意绿色标记的地方,我刚开始没有注意这个,
导致 self.server.transport.write(data)中的server未初始化就开始写数据。
o~了。。
写完后看了看twisted的源码,发现在twisted.protocols.base 下有一个portforword.py。
贴出来:
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
# See LICENSE for details.
"""
A simple port forwarder.
"""
# Twisted imports
from twisted.internet import protocol
from twisted.python import log
class Proxy(protocol.Protocol):
noisy = True
peer = None
def setPeer(self, peer):
self.peer = peer
def connectionLost(self, reason):
if self.peer is not None:
self.peer.transport.loseConnection()
self.peer = None
elif self.noisy:
log.msg("Unable to connect to peer: %s" % (reason,))
def dataReceived(self, data):
self.peer.transport.write(data)
class ProxyClient(Proxy):
def connectionMade(self):
self.peer.setPeer(self)
# We're connected, everybody can read to their hearts content.
self.peer.transport.resumeProducing()
class ProxyClientFactory(protocol.ClientFactory):
protocol = ProxyClient
def setServer(self, server):
self.server = server
def buildProtocol(self, *args, **kw):
prot = protocol.ClientFactory.buildProtocol(self, *args, **kw)
prot.setPeer(self.server)
return prot
def clientConnectionFailed(self, connector, reason):
self.server.transport.loseConnection()
class ProxyServer(Proxy):
clientProtocolFactory = ProxyClientFactory
def connectionMade(self):
# Don't read anything from the connecting client until we have
# somewhere to send it to.
self.transport.pauseProducing()
client = self.clientProtocolFactory()
client.setServer(self)
from twisted.internet import reactor
reactor.connectTCP(self.factory.host, self.factory.port, client)
class ProxyFactory(protocol.Factory):
"""Factory for port forwarder."""
protocol = ProxyServer
def __init__(self, host, port):
self.host = host
self.port = port