码迷,mamicode.com
首页 > 编程语言 > 详细

Python_NAT

时间:2017-06-27 23:33:45      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:ddl   nec   continue   代理服务   ddr   面向   except   erro   star   

sockMiddle_server.py

 1 import sys
 2 import socket
 3 import threading
 4 
 5 #回复消息,原样返回
 6 def replyMessage(conn):
 7     while True:
 8         data = conn.recv(1024)
 9         conn.send(data)
10         if data.decode().lower() == bye:
11             break
12     conn.close()
13 
14 def main():
15     sockScr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
16     sockScr.bind((‘‘, port))
17     sockScr.listen(200)
18     while True:
19         try:
20             conn, addr = sockScr.accept()
21             #只允许特定主机访问本服务器
22             if addr[0] != onlyYou:
23                 conn.close()
24                 continue
25             #创建并启动线程
26             t = threading.Thread(target=replyMessage, args=(conn,))
27             t.start()        
28         except:
29             print(error)
30 
31 if __name__ == __main__:
32     try:
33         #获取命令行参数
34         port = int(sys.argv[1])
35         onlyYou = sys.argv[2]
36         main()
37     except:
38         print(Must give me a number as port)
39         

sockMiddle.py

 1 import sys
 2 import socket
 3 import threading
 4 
 5 def middle(conn, addr):
 6     #面向服务器的Socket
 7     sockDst = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 8     sockDst.connect((ipServer,portServer))
 9     while True:
10         data = conn.recv(1024).decode()
11         print(收到客户端消息:+data)
12         if data == 不要发给服务器:
13             conn.send(该消息已被代理服务器过滤.encode())
14             print(该消息已过滤)
15         elif data.lower() == bye:
16             print(str(addr)+客户端关闭连接)
17             break
18         else:
19             sockDst.send(data.encode())
20             print(已转发服务器)
21             data_fromServer = sockDst.recv(1024).decode()
22             print(收到服务器回复的消息:+data_fromServer)
23             if data_fromServer == 不要发给客户端:
24                 conn.send(该消息已被代理服务器修改.encode())
25                 print(消息已被篡改)
26             else:
27                 conn.send(bServer reply:+data_fromServer.encode())
28                 print(已转发服务器消息给客户端)
29         
30     conn.close()
31     sockDst.close()
32 
33 def main():
34     sockScr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
35     sockScr.bind((‘‘, portScr))
36     sockScr.listen(200)
37     print(代理已启动)
38     while True:
39         try:
40             conn, addr = sockScr.accept()
41             t = threading.Thread(target=middle, args=(conn, addr))
42             t.start()
43             print(新客户:+str(addr))
44         except:
45             pass
46         
47 if __name__ == __main__:
48     try:
49         #(本机IP地址,portScr)<==>(ipServer,portServer)
50         #代理服务器监听端口
51         portScr = int(sys.argv[1])
52         #服务器IP地址与端口号
53         ipServer = sys.argv[2]
54         portServer = int(sys.argv[3])
55         main()
56     except:
57         print(Sth error)

sockMiddle_client.py

 1 import sys
 2 import socket
 3 import threading
 4 
 5 def middle(conn, addr):
 6     #面向服务器的Socket
 7     sockDst = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 8     sockDst.connect((ipServer,portServer))
 9     while True:
10         data = conn.recv(1024).decode()
11         print(收到客户端消息:+data)
12         if data == 不要发给服务器:
13             conn.send(该消息已被代理服务器过滤.encode())
14             print(该消息已过滤)
15         elif data.lower() == bye:
16             print(str(addr)+客户端关闭连接)
17             break
18         else:
19             sockDst.send(data.encode())
20             print(已转发服务器)
21             data_fromServer = sockDst.recv(1024).decode()
22             print(收到服务器回复的消息:+data_fromServer)
23             if data_fromServer == 不要发给客户端:
24                 conn.send(该消息已被代理服务器修改.encode())
25                 print(消息已被篡改)
26             else:
27                 conn.send(bServer reply:+data_fromServer.encode())
28                 print(已转发服务器消息给客户端)
29         
30     conn.close()
31     sockDst.close()
32 
33 def main():
34     sockScr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
35     sockScr.bind((‘‘, portScr))
36     sockScr.listen(200)
37     print(代理已启动)
38     while True:
39         try:
40             conn, addr = sockScr.accept()
41             t = threading.Thread(target=middle, args=(conn, addr))
42             t.start()
43             print(新客户:+str(addr))
44         except:
45             pass
46         
47 if __name__ == __main__:
48     try:
49         #(本机IP地址,portScr)<==>(ipServer,portServer)
50         #代理服务器监听端口
51         portScr = int(sys.argv[1])
52         #服务器IP地址与端口号
53         ipServer = sys.argv[2]
54         portServer = int(sys.argv[3])
55         main()
56     except:
57         print(Sth error)
‘‘‘
启动3个终端cd到文件目录后:各终端执行一条语句
python sockMiddle_server.py 10000 10.2.1.2
python sockMiddle.py 30800 10.2.1.2 10000
python sockMiddle_client.py 10.2.1.2 30800
‘‘‘

Python_NAT

标签:ddl   nec   continue   代理服务   ddr   面向   except   erro   star   

原文地址:http://www.cnblogs.com/cmnz/p/7087460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!