Pythonftp uploads files

Source: Internet
Author: User
Tags ftp file
This article mainly introduces Pythonftp file uploading materials. If you need more information, refer to the following code. If you are interested in python File Uploading through ftp, refer

# Encoding = utf8from ftplib import FTP # load the ftp module IP address = '2017. 240.150.104 'user = 'webmaster @ stchat.cn 'password = '5' filename = 'zhihu.html' path = '/root/Desktop/zhihu.html' ftp = FTP () # Set the variable ftp. set_debuglevel (2) # enable debug level 2 and display the ftp details. connect (IP) # connect to the ftp server and port ftp. login (user, password) # connection username, password print ftp. getwelcome () # print out the welcome information ftp. storbinary ('stor % s' % filename, open (path, 'rb', 8192) print ('success ')

The following describes how to upload and download files through ftp in python.

Python itself comes with an FTP module to implement the upload/download function.

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"), 1024 )#! /Usr/bin/env python #-*-coding: UTF-8-*-from ftplib import FTP def ftp_up (filename = "20120904.rar"): ftp = FTP () ftp. set_debuglevel (2) # enable debugging Level 2 and display details. 0 indicates disabling debugging information ftp. connect ('2017. 168.0.1 ', '21') # connect to ftp. login ('admin', 'admin') # log on. If you log on anonymously, replace it with an empty string. # print ftp. getwelcome () # display ftp server welcome information # ftp. cwd ('xxx/xxx/') # select the operation directory bufsize = 1024 # Set the buffer block size file_handler = open (filename, 'rb') # open the file ftp locally in Read mode. storbinary ('stor % s' % OS. path. basename (filename), file_handler, bufsize) # Upload File 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 ('2017. 168.0.1 ', '21') ftp. login ('admin', 'admin') # print ftp. getwelcome () # display ftp server welcome information # ftp. cwd ('xxx/xxx/') # select the operation directory bufsize = 1024 filename = "20120904.rar" file_handler = open (filename, 'wb '). write # Open the ftp file locally in write mode. retrbinary ('retr % s' % OS. path. basename (filename), file_handler, bufsize) # receives files from the server and writes them to the local file 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.