Python实现TCP/IP协议下的端口转发及重定向示例

所属分类: 脚本专栏 / python 阅读数: 1958
收藏 0 赞 0 分享

首先,我们用webpy写一个简单的网站,监听8080端口,返回“Hello, EverET.org”的页面。

然后我们使用我们的forwarding.py,在80端口和8080端口中间建立两条通信管道用于双向通信。

此时,我们通过80端口访问我们的服务器。

浏览器得到:

2016614172131520.png (426×190)

然后,我们在forwarding.py的输出结果中可以看到浏览器和webpy之间的通信内容。

2016614172157520.png (675×588)

代码:

#!/usr/bin/env python
import sys, socket, time, threading

loglock = threading.Lock()
def log(msg):
  loglock.acquire()
  try:
    print '[%s]: \n%s\n' % (time.ctime(), msg.strip())
    sys.stdout.flush()
  finally:
    loglock.release()

class PipeThread(threading.Thread):
  def __init__(self, source, target):
    threading.Thread.__init__(self)
    self.source = source
    self.target = target

  def run(self):
    while True:
      try:
        data = self.source.recv(1024)
        log(data)
        if not data: break
        self.target.send(data)
      except:
        break
    log('PipeThread done')

class Forwarding(threading.Thread):
  def __init__(self, port, targethost, targetport):
    threading.Thread.__init__(self)
    self.targethost = targethost
    self.targetport = targetport
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.bind(('0.0.0.0', port))
    self.sock.listen(10)
  def run(self):
    while True:
      client_fd, client_addr = self.sock.accept()
      target_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      target_fd.connect((self.targethost, self.targetport))
      log('new connect')
      # two direct pipe
      PipeThread(target_fd, client_fd).start()
      PipeThread(client_fd, target_fd).start()


if __name__ == '__main__':
  print 'Starting'
  import sys
  try:
    port = int(sys.argv[1])
    targethost = sys.argv[2]
    try: targetport = int(sys.argv[3])
    except IndexError: targetport = port
  except (ValueError, IndexError):
    print 'Usage: %s port targethost [targetport]' % sys.argv[0]
    sys.exit(1)

  #sys.stdout = open('forwaring.log', 'w')
  Forwarding(port, targethost, targetport).start()

更多精彩内容其他人还在看

python中seaborn包常用图形使用详解

今天小编就为大家分享一篇python中seaborn包常用图形使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

numpy:找到指定元素的索引示例

今天小编就为大家分享一篇numpy:找到指定元素的索引示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python实现图片上添加图片

这篇文章主要为大家详细介绍了python实现图片上添加图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

从numpy数组中取出满足条件的元素示例

今天小编就为大家分享一篇从numpy数组中取出满足条件的元素示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python实现图片添加文字

这篇文章主要为大家详细介绍了Python实现图片添加文字,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

python实现在多维数组中挑选符合条件的全部元素

今天小编就为大家分享一篇python实现在多维数组中挑选符合条件的全部元素,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python如何使用BeautifulSoup爬取网页信息

这篇文章主要介绍了Python如何使用BeautifulSoup爬取网页信息,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

浅谈python已知元素,获取元素索引(numpy,pandas)

今天小编就为大家分享一篇浅谈python已知元素,获取元素索引(numpy,pandas),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

numpy ndarray 按条件筛选数组,关联筛选的例子

今天小编就为大家分享一篇numpy ndarray 按条件筛选数组,关联筛选的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python pickle模块实现对象序列化

这篇文章主要介绍了Python pickle模块实现对象序列化,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多