Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 912
Jacob Avatar asked May 31 '26 22:05

Jacob


1 Answers

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.

like image 138
Martin Prikryl Avatar answered Jun 03 '26 13:06

Martin Prikryl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!