0

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)
3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Gribouillis
0

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 by Gribouillis: n/a

0

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 by richieking: n/a

0

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 by Gribouillis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.