赞
踩
家里有公网但是没有ICP
外网socket server
接收家中ip去替换nginx模版内的__ip__文字并重新加载
import socket import os # 创建一个TCP socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 获取本地机器的IP和端口 local_ip = '0.0.0.0' local_port = 12345 # 绑定socket到IP和端口 server_socket.bind((local_ip, local_port)) # 设置最大连接数 server_socket.listen(5) last_remote_ip = '' try: while True: client_socket, client_address = server_socket.accept() # 获取远程客户端的IP地址 remote_ip = client_address[0] print(f"Connected by {remote_ip}") client_socket.close() if last_remote_ip != remote_ip: os.system('cp -f /root/nginx.template.conf /root/nginx.new.conf') command = f'sed -i \'s/__ip__/{remote_ip}/g\' /root/nginx.new.conf' print(command); os.system(command) os.system('cp -f /root/nginx.new.conf /etc/nginx/nginx.conf') os.system('service nginx reload') last_remote_ip = remote_ip except KeyboardInterrupt: print("Server stopped.") finally: server_socket.close()
家中socket client
import socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 服务器的IP和端口 server_ip = 'xxxxxx' # 服务器的IP地址 server_port = 12345 # 服务器的端口号 try: client_socket.connect((server_ip, server_port)) message = b'1' # 发送的数据 client_socket.sendall(message) finally: client_socket.close()
这样就可以检测家中ip如果变化, 则修改nginx文件进行重新加载
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。