Moondra
Moondra

Reputation: 4511

Changing directory of FTP server (ftplib, pyftpdlib)

I am attempting to create client/server FTP via Python libraries pyftpdlib and ftplib.

I have created a server as follows:

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import os

authorizer = DummyAuthorizer()
authorizer.add_user("user", "12345", ".",  perm="elradfmw")
authorizer.add_anonymous(os.getcwd())

handler = FTPHandler
handler.authorizer = authorizer

address = ('',1024)
server = FTPServer((address), handler)
server.serve_forever()

I connect to the server:

from ftplib import FTP
import os
ftp = FTP('')
ftp.connect('localhost',1024)
ftp.login(user='user', passwd = '12345')

Which I am able to do as the python console outputs a message "login successful".

Now the problem is I am not sure which directory I am in and how to change directories.

If I use print(ftp.pwd()) I get back:

'/'

Which on Windows is not making much sense to me.

I am assuming it's C:\ but If I try to change directories,

to

ftp.cwd(r"/Users/Moondra/Desktop/")
ftp.cwd(r"Users\Moondra\Desktop")
ftp.cwd(r"\Users\Moondra\Desktop")

I get:

ftplib.error_perm: 550 No such file or directory.

So why am I having trouble changing my directory?

Upvotes: 2

Views: 2235

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202168

You have rooted your user into the directory, where you started your FTP server from:

authorizer.add_user("user", "12345", ".", perm="elradfmw")

. means "this/current working directory". That makes sense for testing purposes only, but not for production use.


If you want to allow the user to access a whole drive, root it there. This should probably do:

authorizer.add_user("user", "12345", "C:\\", perm="elradfmw")

Though for security reasons, you should limit the user.

Maybe like this:

authorizer.add_user("user", "12345", "C:\\Users\\Moondra", perm="elradfmw")

Upvotes: 1

Related Questions