import socket
from select import select
# 创建一个套接字并绑定到V*N服务器的地址和端口
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("your_V*N_server_ip", your_V*N_server_port))
server_socket.listen(5)
# 用于存储客户端和目标服务器之间的连接
connections = {}
while True:
# 使用select来检查哪些套接字准备好进行读取
readable_sockets, _, _ = select(list(connections.values()) + [server_socket], [], [])
for s in readable_sockets:
if s is server_socket:
# 接受新的客户端连接
client_socket, client_address = server_socket.accept()
target_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 连接到目标服务器
target_socket.connect(("target_website_ip", target_website_port))
# 将客户端和目标服务器的套接字存储在字典中
connections[client_socket] = target_socket
connections[target_socket] = client_socket
else:
# 读取数据并将其发送到对应的套接字
data = s.recv(4096)
if data:
connections[s].sendall(data)
else:
# 关闭套接字并从字典中移除
connections[s].close()
connections[connections[s]].close()
del connections[connections[s]]
del connections[s]