Chinaunix首页 | 论坛 | 博客
  • 博客访问: 549384
  • 博文数量: 142
  • 博客积分: 2966
  • 博客等级: 少校
  • 技术积分: 1477
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-07 22:37
文章分类

全部博文(142)

文章存档

2013年(3)

2012年(21)

2011年(53)

2010年(33)

2009年(32)

分类: Python/Ruby

2011-07-08 15:06:53

 

  1. """ This is an incredibly simple port forwarder from port 7000 to 22 on
  2. localhost. It calls a callback function when the socket is closed, to
  3. demonstrate one way that you could start to do interesting things by
  4. starting from a simple framework like this.
  5. """

  6. import eventlet
  7. def closed_callback():
  8.     print "called back"

  9. def forward(source, dest, cb = lambda: None):
  10.     """Forwards bytes unidirectionally from source to dest"""
  11.     while True:
  12.         d = source.recv(32384)
  13.         if d == '':
  14.             cb()
  15.             break
  16.         dest.sendall(d)

  17. listener = eventlet.listen(('localhost', 7000))
  18. while True:
  19.     client, addr = listener.accept()
  20.     server = eventlet.connect(('localhost', 22))
  21.     # two unidirectional forwarders make a bidirectional one
  22.     eventlet.spawn_n(forward, client, server, closed_callback)
  23.     eventlet.spawn_n(forward, server, client)
阅读(707) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~