`
qiezi
  • 浏览: 491508 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Twisted 编写的TCP转向连接程序

阅读更多
机房对外的系统安装的是Linux,内网有一台Windows机器,想找工作把3389端口转到Linux服务器上,每次临时启用。

安装了iptables,竟然提示缺少模块,配置也比较繁琐,所以就放弃了。

想到以前用过的twisted,于是自己写了一个小工具:

from twisted.internet.protocol import Protocol, ClientFactory, ServerFactory
from twisted.internet import reactor
import sys

class ForwardServer(Protocol):
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.data = ""
        self._connected = False

    def dataReceived(self, data):
        #print "Received %d bytes from client\n" % len(data)
        self.data += data
        #print "%d bytes in buffer" % len(self.data)
        if self._connected and (len(self.data) > 0):
            self.connector.transport.write(self.data)
            #print "Sent %d bytes to server" % len(self.data)
            self.data = ""

    def connectionMade(self):
        self.connector = reactor.connectTCP(self.host, self.port, ForwardClientFactory(self))
        print "Client connected"

    def setConnected(self, flag):
        if flag:
            self.onConnected()
        else:
            self.transport.loseConnection()
        self._connected = flag

    def onConnected(self):
        if len(self.data) > 0:
            self.connector.transport.write(self.data)
            self.data = ""

    def connectionLost(self, reason):
        self.connector.transport.loseConnection()
        self._connected = False
        print "Client disonnected"

class ForwardClient(Protocol):
    def __init__(self, forward):
        self.forward = forward

    def dataReceived(self, data):
        #print "Received %d bytes from server\n" % len(data)
        self.forward.transport.write(data)

    def connectionMade(self):
        print "Connected to server"
        self.forward.setConnected(True)

    def connectionLost(self, reason):
        self.forward.setConnected(False)
        print "Disconnected from server"

class ForwardServerFactory(ServerFactory):
    def __init__(self, host, port):
        self.host = host
        self.port = port

    def buildProtocol(self, addr):
        return ForwardServer(self.host, self.port)

class ForwardClientFactory(ClientFactory):
    def __init__(self, forward):
        self.forward = forward

    def buildProtocol(self, addr):
        return ForwardClient(self.forward)

    def clientConnectionFailed(self, connector, reason):
        self.forward.transport.loseConnection()


if __name__ == "__main__":
    if len(sys.argv) != 4:
        print "USAGE: %s <host> <port> <listen port>" % sys.argv[0]
        sys.exit(1)
    host, port, listen_port = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
    server_factory = ForwardServerFactory(host, port)
    reactor.listenTCP(listen_port, server_factory)
    reactor.run()

运行:
python port_forward.py 192.168.0.10 3389 3389

就可以在本机上监听3389端口,有客户连接时就建立起到服务器的连接,并在2台机器间转发数据。

twisted是reactor架构,可以支持多个侦听器和连接器,也可以接受多个客户同时访问。作些修改,就可以改成简单的负载均衡服务器。twisted易用性、效率都是很不错的。
分享到:
评论
1 楼 ggyy 2006-12-28  
就是twisted还没有发布for 2.5,看来我还要等段时间了.

相关推荐

    twisted编写TCP服务器

    Twisted被设计为一个非常灵活的框架,可以使用它编写强大的服务器.这种灵活性的代价是当你编写服务器的时候需要分好几层. 本文档描述协议层,在这里定义协议的解析和处理.如果你正在编写一个应用的话这应该是你读的第...

    python基础教程:使用Python的Twisted框架编写非阻塞程序的代码示例

    Twisted是基于异步模式的开发框架,因而利用Twisted进行非阻塞编程自然也是必会的用法,下面我们就来一起看一下使用Python的Twisted框架编写非阻塞程序的代码示例: Twisted是基于异步模式的开发框架,因而利用Twisted...

    BurstLink, 基于 Twisted的假多连接代理.zip

    BurstLink, 基于 Twisted的假多连接代理 BurstLink一种基于 Twisted的假多连接注释这是一个测试版,可能有 Bug当前不支持支持加密传输安装客户端 Windows安装 python 2.7安装 Twisted 和 zope从下载这里文件

    使用Python的Twisted框架编写非阻塞程序的代码示例

    # ~*~ Twisted - A Python tale ~*~ from time import sleep # Hello, I'm a developer and I mainly setup WordPress. def install_wordpress(customer): # Our hosting company Threads Ltd. is bad. I start ...

    Twisted系列教程.pdf

    第一部分:Twisted理论基础 第二部分:异步编程初探与reactor模式 第三部分:初步认识Twisted 第四部分:由Twisted支持的诗歌客户端 第一个twisted支持的诗歌服务器 第一滴心血 第五部分:由Twisted支持的诗歌...

    Twisted网络编程必备

    Twisted网络编程必备,Twisted是使用Python编写的,强壮的、面向对象的解释性语言。Python使它的爱好者充满热情。...因为Python是跨平台的,所以可以运行Twisted程序在Linux、Windows、Unix和MAC等等系统上。

    Twisted Network Programming Essentials, 2nd Edition

    Twisted supports many common transport and application layer protocols, including TCP, UDP, SSL/TLS, HTTP, IMAP, SSH, IRC, and FTP. Like the language in which it is written, it is “batteries-included...

    Twisted-19.10.0-cp38-cp38.rar

    Twisted是用于Internet应用程序的基于事件的框架,支持Python 2.7和Python 3.5+。它包括用于许多不同目的的模块,其中包括: twisted.web:HTTP客户端和服务器,HTML模板和WSGI服务器 twisted.conch:SSHv2和...

    Twisted Network Programming Essentials 2nd Edition

    Start by building basic TCP clients and servers, and then focus on deploying production-grade applications with the Twisted Application infrastructure. Along the way, you can play with and extend ...

    Twisted-17.1.0-cp36-cp36m-win_amd64.whl

    Twisted的作者试图在当时现有的环境下开发游戏,这一步走的非常艰难,他们迫切地需要一个可扩展性高、基于事件驱动、跨平台的网络开发框架,为此他们决定自己实现一个,并从那些之前的游戏和网络应用程序的开发者中...

    twisted入门教程源码

    Twisted Info Twisted入门教程源码

    Python Twisted-22.10.0-py3-none-any

    Twisted的作者试图在当时现有的环境下开发游戏,这一步走的非常艰难,他们迫切地需要一个可扩展性高、基于事件驱动、跨平台的网络开发框架,为此他们决定自己实现一个,并从那些之前的游戏和网络应用程序的开发者中...

    twisted资料twisted资料

    twisted资料twisted资料twisted资料

    Python twisted教程

    最近有人在twisted邮件列表中问有没有一个可以让人快速学习twisted的文档.总体的来说:这个系列不是这样的一个文档.如果你没有很多时间或者耐心的话,这个系列的文章不太适合你. 不过,如果你对异步编程了解很少的话,...

    Twisted与异步编程入门

    Twisted与异步编程入门,最好的入门资源。

    Python Twisted模块 10.2.0

    Python Twisted模块 10.2.0Python Twisted模块 10.2.0Python Twisted模块 10.2.0Python Twisted模块 10.2.0Python Twisted模块 10.2.0Python Twisted模块 10.2.0

    Twisted Network Programming Essentials

    Twisted Network Programming Essentials,一本介绍socket异步编程框架twisted

    twisted网络通信模型

    twisted网络通信模型

    junkfooder:用Python Twisted编写的IRC机器人

    垃圾食品者 用Python Twisted编写的IRC机器人 这是我们在私人IRC频道上使用的辅助机器人。

Global site tag (gtag.js) - Google Analytics