请选择 进入手机版 | 继续访问电脑版

81路工程师大巴 - 81Lu.Net

 找回密码
 立即注册
搜索
查看: 52|回复: 1
收起左侧

python学习笔记:利用asyncore的端口映射(端口转发)

[复制链接]
发表于 2017-11-5 01:50 | 显示全部楼层 |阅读模式

来不及解释了,快上车,享受更多福利,轻轻松松做大牛!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
python学习笔记_利用asyncore的端口映射(端口转发) - 回头多少个秋 - 博客园.png.png

http://code.activestate.com/recipes/483732/


See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/483730

  for a threaded version. Their UI is better, with a configuration file parser, but with one thread per server socket and two threads per connection, the Python runtime could be under heavy stress as soon as you have more than a few hundred simultaneous connections.

I've tested my version with 252 simultaneous connections (using three Apache Benchmark processes each with 64 concurrent connections) and it handles the load like a charm. asyncore is really an impressive module...

Note that if you need to run more than one forwarder at a time, you just have to build as many forwarder instance as you want before calling asyncore.loop(). In fact, instead of a configuration file, you could just replace the __main__ part with something like :
  1. if __name__ == '__main__':
  2. forwarder('127.0.0.1',80,'www.foobar.com ',80)
  3. forwarder('127.0.0.1',443,'www.foobar.com ',443)
  4. forwarder('127.0.0.1',81,'www.anotherone.com ',80)
  5. forwarder('127.0.0.1',444,'www.anotherone.com ',443)
  6. asyncore.loop()
复制代码


发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请及时给回复的帖子【加分】

当帖子分数用完后,则主题自动结贴,楼主可以返回50%的帖子分数。如果选择【无满意答案结贴】,则结帖后不返分!

添加对方为好友并关注TA,或许会有意想不到的收获!

 楼主| 发表于 2017-11-5 01:59 | 显示全部楼层
  1. import socket,asyncore
  2. class forwarder(asyncore.dispatcher):
  3.     def __init__(self, ip, port, remoteip,remoteport,backlog=5):
  4.         asyncore.dispatcher.__init__(self)
  5.         self.remoteip=remoteip
  6.         self.remoteport=remoteport
  7.         self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
  8.         self.set_reuse_addr()
  9.         self.bind((ip,port))
  10.         self.listen(backlog)
  11.     def handle_accept(self):
  12.         conn, addr = self.accept()
  13.         # print '--- Connect --- '
  14.         sender(receiver(conn),self.remoteip,self.remoteport)
  15. class receiver(asyncore.dispatcher):
  16.     def __init__(self,conn):
  17.         asyncore.dispatcher.__init__(self,conn)
  18.         self.from_remote_buffer=''
  19.         self.to_remote_buffer=''
  20.         self.sender=None
  21.     def handle_connect(self):
  22.         pass
  23.     def handle_read(self):
  24.         read = self.recv(4096)
  25.         # print '%04i -->'%len(read)
  26.         self.from_remote_buffer += read
  27.     def writable(self):
  28.         return (len(self.to_remote_buffer) > 0)
  29.     def handle_write(self):
  30.         sent = self.send(self.to_remote_buffer)
  31.         # print '%04i <--'%sent
  32.         self.to_remote_buffer = self.to_remote_buffer[sent:]
  33.     def handle_close(self):
  34.         self.close()
  35.         if self.sender:
  36.             self.sender.close()
  37. class sender(asyncore.dispatcher):
  38.     def __init__(self, receiver, remoteaddr,remoteport):
  39.         asyncore.dispatcher.__init__(self)
  40.         self.receiver=receiver
  41.         receiver.sender=self
  42.         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  43.         self.connect((remoteaddr, remoteport))
  44.     def handle_connect(self):
  45.         pass
  46.     def handle_read(self):
  47.         read = self.recv(4096)
  48.         # print '<-- %04i'%len(read)
  49.         self.receiver.to_remote_buffer += read
  50.     def writable(self):
  51.         return (len(self.receiver.from_remote_buffer) > 0)
  52.     def handle_write(self):
  53.         sent = self.send(self.receiver.from_remote_buffer)
  54.         # print '--> %04i'%sent
  55.         self.receiver.from_remote_buffer = self.receiver.from_remote_buffer[sent:]
  56.     def handle_close(self):
  57.         self.close()
  58.         self.receiver.close()
  59. if __name__=='__main__':
  60.     import optparse
  61.     parser = optparse.OptionParser()
  62.     parser.add_option(
  63.         '-l','--local-ip',
  64.         dest='local_ip',default='127.0.0.1',
  65.         help='Local IP address to bind to')
  66.     parser.add_option(
  67.         '-p','--local-port',
  68.         type='int',dest='local_port',default=80,
  69.         help='Local port to bind to')
  70.     parser.add_option(
  71.         '-r','--remote-ip',dest='remote_ip',
  72.         help='Local IP address to bind to')
  73.     parser.add_option(
  74.         '-P','--remote-port',
  75.         type='int',dest='remote_port',default=80,
  76.         help='Remote port to bind to')
  77.     options, args = parser.parse_args()
  78.     forwarder(options.local_ip,options.local_port,options.remote_ip,options.remote_port)
  79.     asyncore.loop()
复制代码

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请及时给回复的帖子【加分】

当帖子分数用完后,则主题自动结贴,楼主可以返回50%的帖子分数。如果选择【无满意答案结贴】,则结帖后不返分!

添加对方为好友并关注TA,或许会有意想不到的收获!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|81路工程师大巴 - 81Lu.Net ( 豫ICP备15009715号 )

GMT+8, 2017-11-21 15:50

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表