Python实现TCP/IP协议下的端口转发及重定向示例
作者:Cedric Porter 时间:2021-10-03 04:30:11
首先,我们用webpy写一个简单的网站,监听8080端口,返回“Hello, EverET.org”的页面。
然后我们使用我们的forwarding.py,在80端口和8080端口中间建立两条通信管道用于双向通信。
此时,我们通过80端口访问我们的服务器。
浏览器得到:
然后,我们在forwarding.py的输出结果中可以看到浏览器和webpy之间的通信内容。
代码:
#!/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,端口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python基于Django实现验证码登录功能
2023-06-25 03:21:13
![](https://img.aspxhome.com/file/2023/8/72238_0s.png)
WinHttp.WinHttpRequest--XmlHttp获取跳转页面的功能
2010-03-22 14:34:00
分析SQL语句性能3种方法分享
2012-06-06 20:09:30
python实现一个摇骰子小游戏
2021-11-06 05:49:26
PyQt5+Caffe+Opencv搭建人脸识别登录界面
2022-06-18 01:42:25
![](https://img.aspxhome.com/file/2023/5/103075_0s.jpg)
100 个 Python 小例子(练习题四)
2022-02-15 16:20:05
Python中使用语句导入模块或包的机制研究
2023-02-21 15:06:40
端午节将至,用Python爬取粽子数据并可视化,看看网友喜欢哪种粽子吧!
2023-08-23 06:29:31
![](https://img.aspxhome.com/file/2023/2/62272_0s.png)
解决Python 遍历字典时删除元素报异常的问题
2023-11-17 04:06:47
小三角的做法与使用
2008-10-08 10:25:00
![](https://img.aspxhome.com/file/uploadpic/20071/200713110649890.gif)
python3编写ThinkPHP命令执行Getshell的方法
2023-10-04 14:17:15
![](https://img.aspxhome.com/file/2023/9/78749_0s.jpg)
python多线程semaphore实现线程数控制的示例
2022-12-29 18:31:40
对Python中DataFrame按照行遍历的方法
2023-01-03 23:08:59
有关Server.Mappath详细接触
2010-07-07 11:35:00
![](https://img.aspxhome.com/file/UploadPic/20107/7/2004351085047387-42s.gif)
Python映射类型之dict详解
2023-08-03 13:19:19
python爬取招聘要求等信息实例
2021-01-27 21:22:36
![](https://img.aspxhome.com/file/2023/0/78560_0s.png)
Python入门必须知道的11个知识点
2023-12-19 05:32:47
三步实现Django Paginator分页的方法
2022-09-22 20:15:31
![](https://img.aspxhome.com/file/2023/6/90806_0s.png)
ie和firefox中css自动换行实现方法
2008-04-08 12:49:00
Python tkinter三种布局实例详解
2022-11-28 11:23:44
![](https://img.aspxhome.com/file/2023/7/86417_0s.jpg)