2

I'm using pyftpdlib on python 2.7 as a function that I call from the code. The function is the basic example the pyftpdlib creators has to offer.

The thing is, the module has built-in printings that I want to disable, or at least make them not visible to the user, WITHOUT modifying the module itself.

Help? Anyone?

Thanks in advance, Miri :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Miri Adj
  • 21
  • 4
  • if module use `print()` or `sys.stdout` then you can assign own class to `sys.stdout` to catch all text. But I'm not sure if module will use it. – furas Dec 16 '16 at 18:52
  • how and where should i do it? – Miri Adj Dec 16 '16 at 22:22
  • [Redirect stdout to a file in Python](http://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python) - you can redirect `stdout` to file or on linux to `/dev/null` – furas Dec 16 '16 at 22:34
  • i'm using windows.. but what is the syntax of the redirect? – Miri Adj Dec 17 '16 at 09:21
  • `sys.stdout` is file-like object which has function `write()` (and other like `flush()`, etc.). If you assign different object which has `write()` function then it will use your function to print text - because `print()` uses `sys.stdout.write()` to send text on screen. Loggers may do something like this to send message to screen or to file. – furas Dec 17 '16 at 13:18

1 Answers1

1

pyftpdlib has no print()s. What you see on screen is printed via the logging module. If you want to print ERROR messages only do this before serve_forever():

import logging
from pyftpdlib.log import config_logging
config_logging(level=logging.ERROR)
Giampaolo Rodolà
  • 12,488
  • 6
  • 68
  • 60
  • the thing is, i start it as a thread in one window with my script, means if this module will print anything it will interrupt he usual code. is there a way to disable this completely? – Miri Adj Dec 19 '16 at 17:47
  • config_logging(level=logging.NOTSET) – Giampaolo Rodolà Dec 19 '16 at 21:10
  • so i call my function containing the ftp script, and write this line inside of it? or in my main function? – Miri Adj Dec 20 '16 at 11:24
  • just a quick question, is it possible to set a variable that will keep the output of: config_logging(level=logging.ERROR) inside? or a way to trigger the code to notice i have an error and stop the script? – Miri Adj Dec 20 '16 at 11:47
  • If you only want to see error logs then use config_logging(level=logging.ERROR) as I originally suggested. To call a callback function in case of error is more complicated (you have to override FTPHandler.hande_errror() and DTPHandler.hande_errror() methods). But your use case looks weird. What are you trying to accomplish? – Giampaolo Rodolà Dec 20 '16 at 15:14
  • i am writing a script that will get an ftp file from a remote server and than it will do some actions on it. so i'm running the pyftpdlib as a function that i call. in the script i use prints to print messages to the user, and if i start the ftp function (in a thread) it prints the logs we discussed earlier. with your help i deleted them permanently and thank god it's not visible to the user. the thing is, if the ftp server failed to start, there's no use for the script to continue running, so i want to stop it and print my own error to the screen. how can i check if an error occurred? – Miri Adj Dec 20 '16 at 21:32