Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7403987
  • 博文数量: 1756
  • 博客积分: 18684
  • 博客等级: 上将
  • 技术积分: 16232
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-02 10:28
个人简介

啥也没写

文章分类

全部博文(1756)

文章存档

2024年(2)

2023年(44)

2022年(39)

2021年(46)

2020年(43)

2019年(27)

2018年(44)

2017年(50)

2016年(47)

2015年(15)

2014年(21)

2013年(43)

2012年(143)

2011年(228)

2010年(263)

2009年(384)

2008年(246)

2007年(30)

2006年(38)

2005年(2)

2004年(1)

分类: LINUX

2011-08-10 21:52:59

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

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

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

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

  4. class ForwardServer(Protocol):
  5.     def __init__(self, host, port):
  6.         self.host = host
  7.         self.port = port
  8.         self.data = ""
  9.         self._connected = False

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

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

  21.     def setConnected(self, flag):
  22.         if flag:
  23.             self.onConnected()
  24.         else:
  25.             self.transport.loseConnection()
  26.         self._connected = flag

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

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

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

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

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

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

  47. class ForwardServerFactory(ServerFactory):
  48.     def __init__(self, host, port):
  49.         self.host = host
  50.         self.port = port

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

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

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

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


  60. if __name__ == "__main__":
  61.     if len(sys.argv) != 4:
  62.         print "USAGE: %s " % sys.argv[0]
  63.         sys.exit(1)
  64.     host, port, listen_port = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
  65.     server_factory = ForwardServerFactory(host, port)
  66.     reactor.listenTCP(listen_port, server_factory)
  67.     reactor.run()

运行:
python port_forward.py 192.168.0.10 3389 3389

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

twisted是reactor架构,可以支持多个侦听器和连接器,也可以接受多个客户同时访问。作些修改,就可以改成简单的负载均衡服务器。twisted易用性、效率都是很不错的。

转 :

阅读(667) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~