Re: [pyftpdlib-users] delete user on the fly

75 views
Skip to first unread message
Message has been deleted

Giampaolo Rodola'

unread,
Mar 29, 2017, 1:29:15 PM3/29/17
to pyft...@googlegroups.com
You're using two separate instances of the DummyAuthorizer class.
In fact you instantiate it again in deleteServer() method.
What you have to do instead is keep a reference of the first "authorizer" instance around and invoke remove_user() against that object. 

On Wed, Mar 29, 2017 at 7:25 PM, wichtel <helle...@gmail.com> wrote:
How to delete a user "on the fly", meaning when the server itself is running?
Here's my code at the moment:

Inside my main.py I created a local FTP server using this lib.

Then I create a new folder named modules with a class called GameServer. Inside this class gameserver I defined a method (static) to deleteFTPAccess(username). How to call this method now using the FTP server defined inside the main.py?

My structure of files:

    main.py
       modules
         gameserver.py

gameserver.py

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


   
class GameServer ():


       
def __init__(self, id, ip_address, port):
           
self.id             = id
           
self.ip_address     = ip_address
           
self.port           = port


       
@staticmethod
       
def deleteServer(id):
           
#delete FTP access
            authorizer
= DummyAuthorizer()
            authorizer
.remove_user(id)


main.py

    from pyftpdlib.authorizers import DummyAuthorizer
   
from pyftpdlib.handlers import FTPHandler
   
from pyftpdlib.servers import FTPServer
   
from modules.gameserver import GameServer


    authorizer
= DummyAuthorizer()
    authorizer
.add_user("user", "12345", "/home", perm="elradfmw")
    handler
= FTPHandler
    handler
.authorizer = authorizer
    server
= FTPServer(("127.0.0.1", 21), handler)
    server
.serve_forever()


   
GameServer.deleteServer("user")


When deleting a server it doesn't effect the FTP, meaning I can access the server using the username after deleting. From my point of view, the usage is wrong.

--
You received this message because you are subscribed to the "Python FTP server library" project group:
http://code.google.com/p/pyftpdlib/
To post to this group, send email to pyft...@googlegroups.com
To unsubscribe from this group, send email to pyftpdlib-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/pyftpdlib
---
You received this message because you are subscribed to the Google Groups "Python FTP server library - Discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyftpdlib+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

wichtel

unread,
Mar 29, 2017, 1:32:21 PM3/29/17
to Python FTP server library - Discussion group
Thanks Giampaolo for your quick answer. I'm new to python, sorry for this, but how can I access this from inside my method?
To unsubscribe from this group, send email to pyftpdlib-...@googlegroups.com

For more options, visit this group at http://groups.google.com/group/pyftpdlib
---
You received this message because you are subscribed to the Google Groups "Python FTP server library - Discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyftpdlib+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Giampaolo Rodola'

unread,
Mar 29, 2017, 1:37:51 PM3/29/17
to pyft...@googlegroups.com
You declare the authorized in main.py right?

    authorizer 
= DummyAuthorizer()

All you have to do is to keep it or pass it around so that you can access it.
Judging from the code you pasted you don't need gameserver.py. I'd just keep everything in main.py.
You may want to either:

a) define the "authorizer" object globally, outside of the main() function
b) define it in the main() function and use "global" keyword to make it globally accessible
c) keep it "local" to main() function and pass it to some other class or function as an argument

Point is: just operate on that authorizer and don't instantiate a new one. 


To unsubscribe from this group, send email to pyftpdlib-unsubscribe@googlegroups.com

For more options, visit this group at http://groups.google.com/group/pyftpdlib
---
You received this message because you are subscribed to the Google Groups "Python FTP server library - Discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyftpdlib+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

wichtel

unread,
Mar 29, 2017, 1:48:12 PM3/29/17
to Python FTP server library - Discussion group
I don't want to waste your time, but I used this code inside my main.py

global authorizer

authorizer
= DummyAuthorizer()

and this code is as my delete method inside GameServer class

@staticmethod
def deleteServer(id):
 
#delete FTP access

 authorizer
.remove_user(id)

User is still now removed and when exiting the running code using CTRL + C it says:

NameError: global name 'authorizer' is not defined


Giampaolo Rodola'

unread,
Mar 29, 2017, 1:52:40 PM3/29/17
to pyft...@googlegroups.com
Paste the whole code please.

To unsubscribe from this group, send email to pyftpdlib-unsubscribe@googlegroups.com

For more options, visit this group at http://groups.google.com/group/pyftpdlib
---
You received this message because you are subscribed to the Google Groups "Python FTP server library - Discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyftpdlib+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

wichtel

unread,
Mar 29, 2017, 2:37:32 PM3/29/17
to Python FTP server library - Discussion group
My Gameserver.py

 class GameServer ():


   
def __init__(self, id, ip_address, port, slots, tickrate = 64):

       
self.id             = id
       
self.ip_address     = ip_address
       
self.port           =
port
       
self.slots          = slots
       
self.tickrate       = tickrate


   
@staticmethod

   
def deleteServer(id):
       
#delete FTP access
        authorizer
.remove_user(id)

My main (whole code)

#import ConfigParser
#from database_connection import *

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from modules.gameserver import GameServer

global authorizer

authorizer
= DummyAuthorizer()

authorizer
.add_user("user", "12345", "/home", perm="elradfmw")
handler
= FTPHandler
handler
.authorizer = authorizer
server
= FTPServer(("127.0.0.1", 21), handler)
server
.serve_forever()


GameServer.deleteServer("user")

All the best;

wichtel

unread,
Mar 31, 2017, 2:15:49 PM3/31/17
to Python FTP server library - Discussion group
@Giampaolo Rodola

Is there some way to add/remove users on runtime, if yes can you show me a small example or did I need to stop server, update the handler with new users (excluding the user I want to delete) and then restart the server?

Giampaolo Rodola'

unread,
Apr 1, 2017, 7:42:24 AM4/1/17
to pyft...@googlegroups.com
As I said, you have to operate on the same "authorizer" object.
In order to add and remove users you already have add_user() and remove_user() methods.
Just get rid of the two separate files, put all your code in main.py and keep the authorizer object around (make it global).

--
You received this message because you are subscribed to the "Python FTP server library" project group:
http://code.google.com/p/pyftpdlib/
To post to this group, send email to pyft...@googlegroups.com
To unsubscribe from this group, send email to pyftpdlib-unsubscribe@googlegroups.com

For more options, visit this group at http://groups.google.com/group/pyftpdlib
---
You received this message because you are subscribed to the Google Groups "Python FTP server library - Discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyftpdlib+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

wichtel

unread,
Apr 1, 2017, 12:09:49 PM4/1/17
to Python FTP server library - Discussion group
Here's my full source code at the moment:

main.py

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import MultiprocessFTPServer
from modules.gameserver import Gameserver

def main():

   
print("hallo1")
    authorizer
= DummyAuthorizer()
    authorizer
.add_user('user', '12345', '.')

    handler
= FTPHandler
    handler
.authorizer =
authorizer
    server
= MultiprocessFTPServer(('', 2121), handler)
    server
.serve_forever()
   
print("hallo")
    server
.close_all()

   
Gameserver.deleteServer(server, authorizer, "user")

if __name__ == "__main__":
    main
()

gameserver.py

class Gameserver (): def __init__(self, id, ip_address, port, slots, tickrate = 64): self.id = id self.ip_address = ip_address self.port = port self.slots = slots self.tickrate = tickrate @staticmethod def deleteServer (server, authorizer, name ): authorizer.remove_user(name)

How would I need to "assign" the authorizer object inside my deleteServer method now to the running server?
-I'm running the server multithreaded, so I can execute code while server is running.
To unsubscribe from this group, send email to pyftpdlib-...@googlegroups.com

For more options, visit this group at http://groups.google.com/group/pyftpdlib
---
You received this message because you are subscribed to the Google Groups "Python FTP server library - Discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyftpdlib+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages