Python ftp upload __python

Source: Internet
Author: User

server.py file:

#!/usr/bin/env python #coding: utf-8 Import Socketserver,os class MyServer (Socketserver.baserequesthandler): def handle
        (self): Base_path = R ' C:\Users\91135\Desktop\test ' conn = self.request print ' Connected ... ' While true:pre_data = CONN.RECV (1024) #获取请求方法, filename, file size cmd,file_name,file_size = PR E_data.split ('/') #已经接收文件的大小 recv_size = 0 #上传文件路径拼接 file_dir = os.path.jo
                In (base_path,file_name) f = file (File_dir, ' WB ') Flag = True while Flag: #未上传完毕, if int (file_size) >recv_size: #最多接收1024, may receive less than 1024 da
                    TA = conn.recv (1024) recv_size+=len (data) #上传完毕, exit loop else:
                #recv_size = 0 Flag = False Continue #写入文件
      F.write (data)      print ' Upload successed. '    F.close () instance = socketserver.threadingtcpserver (' 127.0.0.1 ', 9999), MyServer) Instance.serve_forever ()

client.py file:

#!/usr/bin/env python
#coding: utf-8
import socket,os
ip_port = (' 127.0.0.1 ', 9999)
SK = Socket.socket ()
sk.connect (ip_port)
#container = {' key ': ', ' Data ': '} while
True:
    input = raw_input (' cmd and path: ') #cmd和path之间用 '/' split
    Cmd,path = Input.split ('/') 
    file_name = os.path.basename (path)
    File_size=os.stat ( Path). St_size
    Sk.send (cmd+ "/" +file_name+ '/' +str (file_size))
    send_size = 0
    f= file (path, ' RB ')
    Flag = True while
    Flag:
        if send_size + 1024 >file_size:
            data = F.read (file_size-send_size)
            Flag = False
        Else:
            data = f.read (1024)
            send_size+=1024
        sk.send (data)
    f.close ()
    
sk.close ()






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.