EzDevInfo.com

pyftpdlib

Extremely fast and scalable Python FTP server library

Run pyftpdlib server in "background"

this is probably a newbie question.

I'm currently writing a script which logs into appliances and creates files (system dumps). This is working file and the next step is to get those files onto the machine where I started the script. For several reasons, the only way to do this (without setting too many pre-requisites like a running and configured ssh server, etc), I need to run a ftp server within python.

I played a bit with pyftpdlib and the server bit itself is working fine, but my problem is that the whole script stops processing once the server is started and continues after I stopped it (using control-c)

I need to start the ftp server with pyftpdlib and have it running in the background, continue to do the other stuff and when finished, stop the ftp server.

I searched the net, asked aunt google but still no luck.

any help is highly appreciated, thanks.


Source: (StackOverflow)

selectively setting the console logging level

I am trying to use an FTP server stub during tests. I don't want the console output, but I would like to capture the logging to a file.

I want the FTP server to run in a different process, so I use multiprocessing.

My code as follows sets all logging to level WARNING:

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
import pyftpdlib.log as pyftpdliblog
import os
import logging
import multiprocessing as mp

authorizer = DummyAuthorizer()
authorizer.add_user('user', '12345', '.', perm='elradfmwM')
handler = FTPHandler
handler.authorizer = authorizer

pyftpdliblog.LEVEL = logging.WARNING
logging.basicConfig(filename='pyftpd.log', level=logging.INFO)
server = FTPServer(('', 2121), handler)

def main():
    p = mp.Process(target=server.serve_forever)
    p.start()


if __name__ == '__main__':
    main()

How do I set only the console logging to level WARNING, or even better, completely shutdown without giving up the file logging?


Source: (StackOverflow)

Advertisements

Polling every 50 seconds in multithreaded environment

I'm using the following lib for creating ftp server called pyftpdlib.
Following the exact example of building a base ftp server apart of the fact that I've created my own custom handler and passing it as in the example.

First question: I need to be able to poll every 50 seconds on a static dictionary (class variable) defined in the custom handler. After each interval of 50 seconds I need to check for some condition and act accordingly.

As you probably see ,the custom handler is being used by the FTP server, I guess I need some working thread that will be able poll on the custom handler's static dictionary every 50 seconds, how do I launch another thread that will be bound to the custom handler and do that kind of timer polling?

Second question: Is there some way to know that a time of 50 seconds has been passed (on the second it passed) since a specific timestamp which was read from the static dict - I mean to do that without polling every X seconds to check if time has passed, which can be inaccurate.


Source: (StackOverflow)

List files with pyftp - proftpd vs. pyftpdlib behavior

I have a test code which uses an FTP stub with pyftpdlib, which to my surprise failed in production. The reason for this is that proftpd returns the directory name in response to NLST. Here is the response from pyftpdlib FTP stub:

In [10]: local_conn.login('user', '12345')
Out[10]: '230 Login successful.'

In [11]: import ftplib

In [12]: local_conn = ftplib.FTP()

In [13]: local_conn.connect('localhost', 2121)
Out[13]: '220 pyftpdlib 1.4.0 ready.'

In [14]: local_conn.login('user', '12345')
Out[14]: '230 Login successful.'

In [15]: local_conn.nlst('structuredata_advanced')
Out[15]: 
['Report_20150618.csv',
 'Report_20150618.fin',
 'Report_20150619.csv',
 'Report_20150619.fin',
 'Report_20150620.csv',
 'Report_20150620.fin']

Here is the response from proftpd:

In [16]: remote_conn = ftplib.FTP()

In [17]: remote_conn.connect('A1B.7Y.XX.XX', 21)
Out[17]: '220 ProFTPD 1.3.4a Server (vztd3.company.com) [A1B.7Y.XX.XX]'

In [18]: remote_conn.login('remoteuser', 'verysecret')
Out[18]: '230 User yougov logged in'

In [19]: remote_conn.nlst('structuredata_advanced')
Out[19]: 
['structuredata_advanced/Report_20150624.csv',
 'structuredata_advanced/Report_20150629.csv',
 'structuredata_advanced/Report_20150625.fin',
 'structuredata_advanced/Report_20150628.fin',
 'structuredata_advanced/Report_20150627.fin',
 'structuredata_advanced/Report_20150620.fin',
 'structuredata_advanced/Report_20150619.csv', 
  ...]

It's easy enough to remove those directory names:

    # this code works both in production and testing 
    files = conn.nlst(basedir)
    # proftd is weired it returns the basedir name too
    files = [f.split('/')[-1] for f in files]

but I would like to understand if this is something that pyftpdlib does wrong?
Is this something that can be configured in ProFTPD?
Is there something I need to know about the FTP protocol and NLST command?

update

I just tested another ftp server called uftpd it behaves like pyftpdlib when issuing NLST.


Source: (StackOverflow)