Python FTP upload file

Source: Internet
Author: User
Tags ftp upload file
The following code is relatively simple, the Python implementation of the FTP upload files related knowledge of interested friends can refer to the following

#encoding =utf8from ftplib Import FTP #加载ftp模块IP = ' 103.240.150.104 ' user = ' webmaster@stchat.cn ' password = ' 5 ' filename = ' Z hihu.html ' path = '/root/desktop/zhihu.html ' ftp=ftp () #设置变量ftp. Set_debuglevel (2) #打开调试级别2, show details Ftp.connect (IP) # The connected FTP sever and Port Ftp.login (User,password) #连接的用户名, the password print ftp.getwelcome () #打印出欢迎信息ftp. Storbinary (' STOR%s '%filename, Open (Path, ' RB ', 8192)) print (' Success ')

The following is to introduce the Python implementation of FTP upload download file method

Python itself comes with an FTP module, which enables the function of uploading and downloading functions.

Import ftplib ftp = Ftblib. FTP ("ftp.yourServer.com") Ftp.login ("username", "password") filename = "index.html" Ftp.storlines ("STOR" +filename, Open (filename)) filename= "App.exe" Ftp.storbinary ("STOR" +filename,open (filename, "RB"), 1024x768) #!/usr/bin/env python # -*-coding:utf-8-*-from ftplib import FTP def ftp_up (filename = "20120904.rar"): Ftp=ftp () ftp.set_debuglevel (2) #打开调试级 2, show details, 0 to turn off debug information ftp.connect (' 192.168.0.1 ', ' $ ') #连接 ftp.login (' admin ', ' admin ') #登录, if anonymous login is replaced by an empty string #print Ftp.getwelcome () #显示ftp服务器欢迎信息 #ftp. CWD (' xxx/xxx/') #选择操作目录 bufsize = 1024x768 #设置缓冲块大小 file_handler = open (filename, ' RB ') #以 Read mode opens the file locally ftp.storbinary (' STOR%s '% os.path.basename (filename), file_handler,bufsize) #上传文件 ftp.set_debuglevel (0) File_handler.close () ftp.quit () print "ftp up OK" def ftp_down (filename = "20120904.rar"): Ftp=ftp () Ftp.set_debuglevel (2 Ftp.connect (' 192.168.0.1 ', ' + ') ftp.login (' admin ', ' admin ') #print ftp.getwelcome () #显示ftp服务器欢迎信息 #ftp. CWD (' xxx/xxx /') #选择操作目录 bufsize = 1024x768 filename = "20120904.rar "File_handler = open (filename, ' WB '). Write #以写模式在本地打开文件 ftp.retrbinary (' RETR%s '% os.path.basename ( filename), file_handler,bufsize) #接收服务器上文件并写入本地文件 ftp.set_debuglevel (0) File_handler.close () ftp.quit () print "ftp  Down OK "
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.