pythonwindowsftpfirewallwindows-firewall

How to make Python automatically allow port through Windows firewall


I have an FTP server which I would like to be able to send to my personal Windows 10 computers stationed around my area (different IPs) to access files, and in order to access them, I need to allow the ports through the firewalls. Instead of doing this, is there any way to have my Python program use some other port that doesn't need to bypass the firewall OR bypass the firewall altogether?

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()

Solution

  • I'm not aware of any native Python way to configure Windows firewall.

    Though you can simply execute Windows netsh command from Python using os.system.

    See How to open ports on Windows firewall through batch file.