首页新闻找找看学习计划

python用dispather写一个端口转发器的问题

0
悬赏园豆:30 [已解决问题] 解决于 2015-07-20 08:24

本地主机某端口接收到的所有流量重定向到百度首页。不成功,

基本思路PortForwarder类在一个本地套接字中保存进入的客户端请求,然后把这个套接字传给Sender类实例,再使用Receiver类实例发起与远程主机制定端口之间的双向通信。

测试详细见图,为什么出现这样的情况。 出错~

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 
 4 import argparse
 5 import asyncore
 6 import socket
 7 
 8 LOCAL_SERVER_HOST = 'localhost'
 9 REMOTE_SERVER_HOST = 'www.baidu.com'
10 BUFSIZE = 4096
11 
12 class PortForwarder(asyncore.dispatcher):
13     def __init__(self, ip, port, remoteip, remoteport, backlog=5):
14         asyncore.dispatcher.__init__(self)
15         self.remoteip = remoteip
16         self.remoteport = remoteport
17         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
18         self.set_reuse_addr()
19         self.bind((ip, port))
20         self.listen(backlog)
21 
22     def handle_accept(self):
23         conn, addr = self.accept()
24         print "Connected to:", addr
25         Sender(Receiver(conn), self.remoteip, self.remoteport)
26 
27 class Receiver(asyncore.dispatcher):
28     def __init__(self, conn):
29         asyncore.dispatcher.__init__(self, conn)
30         self.from_remote_buffer = ''
31         self.to_remote_buffer = ''
32         self.sender = None
33 
34     def handle_connect(self):
35         pass
36 
37     def handle_read(self):
38         read = self.recv(BUFSIZE)
39         print "receiver read", read
40         self.from_remote_buffer += read;
41 
42     def writable(self):
43         return (len(self.to_remote_buffer) > 0)
44 
45     def handle_write(self):
46         sent = self.send(self.to_remote_buffer)
47         print "receiver write",sent
48         self.to_remote_buffer = self.to_remote_buffer[sent:]
49 
50     def handle_close(self):
51         self.close()
52         if self.sender:
53             self.sender.close()
54 
55 class Sender(asyncore.dispatcher):
56     def __init__(self, receiver, remoteaddr, remoteport):
57         asyncore.dispatcher.__init__(self)
58         self.receiver = receiver
59         receiver.sender = self
60         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
61         self.connect((remoteaddr, remoteport))
62 
63     def handle_connect(self):
64         pass
65 
66     def handle_read(self):
67         read = self.recv(BUFSIZE)
68         print "sender read",read
69         self.receiver.to_remote_buffer += read
70 
71     def writable(self):
72         return (len(self.receiver.from_remote_buffer) > 0)
73 
74     def handle_write(self):
75         sent = self.send(self.receiver.from_remote_buffer)
76         print "sender read",sent
77         self.receiver.from_remote_buffer = self.receiver.from_remote_buffer[sent:]
78 
79     def handle_close(self):
80         self.close()
81         self.receiver.close()
82 
83 if __name__ == '__main__':
84     parser = argparse.ArgumentParser(description="stackless socket server example")
85     parser.add_argument('--localhost', action="store", dest="local_host", default=LOCAL_SERVER_HOST)
86     parser.add_argument('--local-port', action="store", dest="local_port", type=int, required=True)
87     parser.add_argument('--romote-port', action="store", dest="remote_host",default=REMOTE_SERVER_HOST)
88     parser.add_argument('--remot-port', action="store", dest="remote_port", type=int, default=80)
89     given_args = parser.parse_args()
90     local_host, remote_host = given_args.local_host, given_args.remote_host
91     local_port, remote_port = given_args.local_port, given_args.remote_port
92     print "start port forwarding local %s:%s => remote %s:%s" % \
93         (local_host, local_port, remote_host, remote_port)
94     PortForwarder(local_host, local_port, remote_host, remote_port)
95     asyncore.loop()

TinDin的主页 TinDin | 初学一级 | 园豆:126
提问于:2015-07-17 12:18
< >
分享
最佳答案
0

本地发送的HTTP请求不对,把host改成了远程主机即可解决

TinDin | 初学一级 |园豆:126 | 2015-07-20 08:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册