Python_NAT

sockMiddle_server.py

 import sys
import socket
import threading #回复消息,原样返回
def replyMessage(conn):
while True:
data = conn.recv(1024)
conn.send(data)
if data.decode().lower() == 'bye':
break
conn.close() def main():
sockScr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockScr.bind(('', port))
sockScr.listen(200)
while True:
try:
conn, addr = sockScr.accept()
#只允许特定主机访问本服务器
if addr[0] != onlyYou:
conn.close()
continue
#创建并启动线程
t = threading.Thread(target=replyMessage, args=(conn,))
t.start()
except:
print('error') if __name__ == '__main__':
try:
#获取命令行参数
port = int(sys.argv[1])
onlyYou = sys.argv[2]
main()
except:
print('Must give me a number as port')

sockMiddle.py

 import sys
import socket
import threading def middle(conn, addr):
#面向服务器的Socket
sockDst = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockDst.connect((ipServer,portServer))
while True:
data = conn.recv(1024).decode()
print('收到客户端消息:'+data)
if data == '不要发给服务器':
conn.send('该消息已被代理服务器过滤'.encode())
print('该消息已过滤')
elif data.lower() == 'bye':
print(str(addr)+'客户端关闭连接')
break
else:
sockDst.send(data.encode())
print('已转发服务器')
data_fromServer = sockDst.recv(1024).decode()
print('收到服务器回复的消息:'+data_fromServer)
if data_fromServer == '不要发给客户端':
conn.send('该消息已被代理服务器修改'.encode())
print('消息已被篡改')
else:
conn.send(b'Server reply:'+data_fromServer.encode())
print('已转发服务器消息给客户端') conn.close()
sockDst.close() def main():
sockScr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockScr.bind(('', portScr))
sockScr.listen(200)
print('代理已启动')
while True:
try:
conn, addr = sockScr.accept()
t = threading.Thread(target=middle, args=(conn, addr))
t.start()
print('新客户:'+str(addr))
except:
pass if __name__ == '__main__':
try:
#(本机IP地址,portScr)<==>(ipServer,portServer)
#代理服务器监听端口
portScr = int(sys.argv[1])
#服务器IP地址与端口号
ipServer = sys.argv[2]
portServer = int(sys.argv[3])
main()
except:
print('Sth error')

sockMiddle_client.py

 import sys
import socket
import threading def middle(conn, addr):
#面向服务器的Socket
sockDst = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockDst.connect((ipServer,portServer))
while True:
data = conn.recv(1024).decode()
print('收到客户端消息:'+data)
if data == '不要发给服务器':
conn.send('该消息已被代理服务器过滤'.encode())
print('该消息已过滤')
elif data.lower() == 'bye':
print(str(addr)+'客户端关闭连接')
break
else:
sockDst.send(data.encode())
print('已转发服务器')
data_fromServer = sockDst.recv(1024).decode()
print('收到服务器回复的消息:'+data_fromServer)
if data_fromServer == '不要发给客户端':
conn.send('该消息已被代理服务器修改'.encode())
print('消息已被篡改')
else:
conn.send(b'Server reply:'+data_fromServer.encode())
print('已转发服务器消息给客户端') conn.close()
sockDst.close() def main():
sockScr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockScr.bind(('', portScr))
sockScr.listen(200)
print('代理已启动')
while True:
try:
conn, addr = sockScr.accept()
t = threading.Thread(target=middle, args=(conn, addr))
t.start()
print('新客户:'+str(addr))
except:
pass if __name__ == '__main__':
try:
#(本机IP地址,portScr)<==>(ipServer,portServer)
#代理服务器监听端口
portScr = int(sys.argv[1])
#服务器IP地址与端口号
ipServer = sys.argv[2]
portServer = int(sys.argv[3])
main()
except:
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
'''
上一篇:(转载)Google的PageRank算法


下一篇:结合JDK源码看设计模式——简单工厂、工厂方法、抽象工厂