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

时间:2019-03-31 00:12:35

标签: python windows ftp firewall windows-firewall

我有一个FTP服务器,我希望能够将其发送到位于我所在区域(不同IP)周围的个人Windows 10计算机以访问文件,并且要访问它们,我需要允许端口通过防火墙。除了执行此操作,还有什么方法可以让我的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()

1 个答案:

答案 0 :(得分:1)

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

尽管您可以简单地使用Windows netsh command从Python执行os.system

请参见How to open ports on Windows firewall through batch file