i am building a simple sync program,
whan the program starts it takes all the bandwith,
i need to set the speed limit,
i am using using ftplib

file = open(local_file_path, 'r')
    print ftp.storlines('STOR '+ file_name , file)

You could use a modified file object which sleeps between the lines. The following code works for me

# python 2
HOST, USER, PASSWD = "", "", "" # <--- your values here

from ftplib import FTP
from time import sleep

class SlowFile(object):
    def __init__(self, name, mode="r", delay = 0.1):
        self.src = open(name, mode)
        self.delay = delay
        
    def readline(self):
        line = self.src.readline()
        if line:
            sleep(self.delay) # sleep between the lines
        return line

def main():
    ftp = FTP(HOST, USER, PASSWD)
    try:
        name = "foo.txt"
        src = SlowFile(name, "r", delay = 0.2)
        ftp.storlines("STOR " + name, src)
    finally:
        ftp.quit()
    
if __name__ == "__main__":
    main()

This should be very slow. You may set the delay according to your file's size.

Edited 5 Years Ago by Gribouillis: n/a

Grib. SlowFile.readline was not called therefore the delay will not work.
it should be like......

def main():
    ftp = FTP(HOST, USER, PASSWD)
    try:
        name = "foo.txt"
        src = SlowFile(name, "r", delay = 0.2)
        ftp.storlines("STOR " + name, src.readline()) # this part....
    finally:
        ftp.quit()
    
if __name__ == "__main__":
    main()

Edited 5 Years Ago by richieking: n/a

Grib. SlowFile.readline was not called therefore the delay will not work.
it should be like......

def main():
    ftp = FTP(HOST, USER, PASSWD)
    try:
        name = "foo.txt"
        src = SlowFile(name, "r", delay = 0.2)
        ftp.storlines("STOR " + name, src.readline()) # this part....
    finally:
        ftp.quit()
    
if __name__ == "__main__":
    main()

I don't think so: first, the code worked for me as it is, second, the second argument of ftp.storlines() is an open file object with a readline() method, according to the python documentation. In your snippet, you're passing a string (the first line of the file).

Edited 5 Years Ago by Gribouillis: n/a

This article has been dead for over six months. Start a new discussion instead.