python ddns nginx pass

家里有公网但是没有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文件进行重新加载

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值