admin管理员组

文章数量:1333439

I am building a FTP server for a project and I want to use self signed certificate. For this I am using pyftpdlib library and importing TLS_FTPHandler from it. But VS code is not accepting it.

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler,TLS_FTPHandler
from pyftpdlib.servers import FTPServer
import ssl

def run_secure_ftps_server():
    # Set up user authorization
    authorizer = DummyAuthorizer()
    authorizer.add_user("ftp_user", "ftp_password", ".", perm="elradfmw")
    authorizer.add_anonymous(".", perm="elr")  # Optional: Anonymous login with limited access

    # Create an FTPS handler
    handler = TLS_FTPHandler
    handler.authorizer = authorizer

    # Set up SSL/TLS for FTPS
    certfile = "server.pem"  # Path to your SSL certificate
    ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
    ssl_context.load_cert_chain(certfile)
    handler.ssl_context = ssl_context

    # Force encryption on control and data channels
    handler.tls_control_required = False
    handler.tls_data_required = True

    # Start the FTPS server
    ftps_server = FTPServer(("0.0.0.0", 21), handler)
    print("FTPS Server running on port 2121 with secure TLS...")
    ftps_server.serve_forever()

if __name__ == "__main__":
    # Generate a self-signed certificate if it doesn't exist
    import os
    if not os.path.exists("server.pem"):
        print("Generating self-signed SSL certificate...")
        os.system("openssl req -new -x509 -days 365 -nodes -out server.pem -keyout server.pem")

    run_secure_ftps_server()

VS code is giving the reply as

from pyftpdlib.handlers import FTPHandler,TLS_FTPHandler
ImportError: cannot import name 'TLS_FTPHandler' from 'pyftpdlib.handlers' (C:\Users\Faaez\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyftpdlib\handlers.py). Did you mean: 'FTPHandler'?

I have seen all the documentation and the version of all the libraries online but still no clue. I have asked diff AI models and online forums but still no clue. Because of this, I can't connect the client to server. Though, I am using the same versions.

本文标签: pythonProblem in TLSFTPHandler fom pyftpdlib libraryStack Overflow