如何使 Python 自动允许端口通过 Windows 防火墙

How to make Python automatically allow port through Windows firewall

我有一个 FTP 服务器,我希望能够将其发送到我个人的 Windows 10 台位于我所在区域(不同 IP)的计算机以访问文件,并为了访问他们,我需要允许端口通过防火墙。除了这样做,还有什么方法可以让我的 Python 程序使用不需要绕过防火墙或完全绕过防火墙的其他端口?

Server.py

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import urllib.request

import mysql.connector
sqlpass = ""
version = "1.3"

def ftp_main():
    mydb = mysql.connector.connect(
        host="",
        port="3306",
        user="",
        passwd=sqlpass,
        database=""
    )
    mycursor = mydb.cursor()

    mycursor.execute("SELECT Username, Password FROM FtpInfo")
    myresult = mycursor.fetchall()
    Username, Password = myresult[0]
    print(Username + " " + Password)

    external_ip = urllib.request.urlopen('https://ident.me').read().decode('utf8')

    print(external_ip)
    authorizer = DummyAuthorizer()

    # Define a new user having full r/w permissions and a read-only
    # anonymous user
    authorizer.add_user(Username, Password, '.', perm='elradfmwMT')
    authorizer.add_anonymous('.')

    # Instantiate FTP handler class
    handler = FTPHandler
    handler.authorizer = authorizer
    handler.masquerade_address = external_ip
    handler.passive_ports = range(60000, 60999)

    # Define a customized banner (string returned when client connects)
    handler.banner = "FTP Server v" + version

    address = ('', 1000)
    server = FTPServer(address, handler)

    # start ftp server
    server.serve_forever()

ftp_main()

我不知道有任何本机 Python 配置 Windows 防火墙的方法。

尽管您可以简单地执行 Windows netsh command from Python using os.system

How to open ports on Windows firewall through batch file