Python transfers data to win8's FTP protocol (Python ftp upload files)

from ftplib import FTP 


def upload(f, local_path,remote_path): 
    fp = open(local_path, "rb") 
    buf_size = 4096 
    f.storbinary("STOR {}".format(remote_path), fp, buf_size) 
    fp.close( ) 


def download(f, remote_path, local_path): 
    fp = open(local_path, "wb") 
    buf_size = 1024 
    f.retrbinary('RETR {}'.format(remote_path), fp.write, buf_size) 
    fp.close() 


if __name__ == "__main__": 
    ftp = FTP() 
    ftp.connect("ip", 21) # The first parameter can be the ip or domain name of the ftp server, and the second parameter is the connection port of the ftp server. The default is 21 
    ftp.login() # Anonymous login directly uses ftp.login() 
    # ftp.login('user','passwd') # Anonymous login directly uses ftp.login() 
    ftp.set_pasv(False)
    
    upload(ftp, "a.txt", "p_a.txt") # Upload the a.txt file in the current directory to the tmp directory of the ftp server, named ftp_a.txt 
    # download(ftp, "p_a.txt", "b.txt") # Download the ftp_a.txt file in the tmp directory of the ftp server to the current directory and name it b.txt 
    print('yes ok!') 
    ftp.quit()

Guess you like

Origin blog.csdn.net/Hodors/article/details/115066438