Python Simple FTP server

Source: Internet
Author: User

Function Description: Implement Simple file upload and download function

The program is divided into three parts:

①ftp_opr.py upload and download function, need to be together with other programs

②ftp_server.py Service-side program

③ftp_client.py client program


#!/usr/bin/env python# -*- coding:utf-8 -*-#filename: Ftp_opr.pyimport socket,os, Jsondef send_file (socket_object,filename):       ### #传入套接字对象和文件名, hereinafter      fk = open (filename,  ' RB ')     file_size =  Os.path.getsize (filename)     dict = {' filename ': filename, ' file_size ': file_size}     socket_object.send (Json.dumps (dict) encode (' Utf-8 '))     print (' Ready to send the file name: ', dict[' filename '], ' File size: ', dict[' file_size '])     socket_object.recv (1024x768)    ### #等待一个save_file发过来的无用信号 for preventing Sticky pack     socket_object.send (Fk.read ())      fk.close () def save_file (socket_object,filename):    name_size_dict =  Json.loads (SOCKET_OBJECT.RECV (1024x768). Decode (' Utf-8 '))     print (' Ready to receive file name: ', name_size_dict[' FileName '], ' File size: ',  name_size_dict[' file_size ')     socket_object.send (b ' ready ')   ### #发送一个无用的信号 to prevent sticky packets      #filename  = name_size_dict[' filename ']    #### Calling this method requires the filename parameter to be entered, and the dict already contains the filename, so ignore one     file_size = name_size_dict [' File_size ']    fk = open (filename+ '. New ',  ' WB ')      receive_size = 0    while file_size > receive_size:         DATA = SOCKET_OBJECT.RECV (1024x768)          fk.write (data)         receive_size +=  len (data)         print (len (data),  receive_size, file_ Size)     print (' File received! ')     fk.close ()
#!/usr/bin/env python# -*- coding:utf-8 -*-#filename:ftp_server.pyfrom ftp_opr  Import send_file,save_filedef ftp_server ():     sk = socket.socket ()     sk.bind ((' 0.0.0.0 ', 9999))     ### #绑定本机网卡地址和端口      sk.listen ()     while true:        conn, Addr = sk.accept ()         print (' New connection: ', addr)          while True:             CMD = CONN.RECV (1024x768). Decode (' Utf-8 ')              if len (CMD)  == 0:                 print (' client disconnected ')                  break            elif  Cmd.split () [0] ==  ' get ':     ### #对应客户端下载功能                  filename= cmd.split () [1]                 print (CMD)                  send_file (Conn, filename)             elif cmd.split () [0] ==  ' Put ':     ### #对应客户端上传功能                  filename= cmd.split () [1]                 print (CMD)               &nBsp;  save_file (Conn, filename)              else:                  ' Reserved function section '                  pass    sk.close () if __name__ ==  ' __main__ ':     Ftp_server ()
#!/usr/bin/env python# -*- coding:utf-8 -*-#filename: Ftp_client.pyimport socket,os, Jsonfrom ftp_opr import send_file,save_filedef ftp_client ():     sk  = socket.socket ()     sk.connect ((' 127.0.0.1 ', 9999))     #### Connecting the server address and Port     while true:        cmd =  input (' >>>: ')         if len (cmd)  == 0: continue        elif cmd ==  ' Q ':             exit ()          Elif len (Cmd.split ())  > 2:             print (' Too many input parameters, use  put/get + 1 file name ')         elif  cmd.split () [0] ==  ' Get ':     ### #下载功能              Filename= cmd.split () [1]            sk.send ( Bytes (Cmd.encode (' Utf-8 ')))             save_file ( Sk, filename)         elif cmd.split () [0] ==  ' put ':     ### #下载功能              Filename= cmd.split () [1]            sk.send ( Bytes (Cmd.encode (' Utf-8 ')))             send_file ( Sk, filename)         else:             print (' Input error, please enter  put/get +  filename ')         &nbsP;    pass    sk.close () if __name__ ==  ' __main__ ':     ftp_client ()

Python simple FTP server

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.