python ftp上傳__python

來源:互聯網
上載者:User

server.py檔案:

#!/usr/bin/env python#coding:utf-8import SocketServer,osclass 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)            #擷取要求方法、檔案名稱、檔案大小            cmd,file_name,file_size = pre_data.split('/')            #已經接收檔案的大小            recv_size = 0            #上傳檔案路徑拼接            file_dir = os.path.join(base_path,file_name)            f = file(file_dir,'wb')            Flag = True            while Flag:                #未上傳完畢,                if int(file_size)>recv_size:                    #最多接收1024,可能接收的小於1024                    data = conn.recv(1024)                     recv_size+=len(data)                #上傳完畢,則退出迴圈                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檔案:

#!/usr/bin/env python#coding:utf-8import socket,osip_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之間用'/'分割    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()






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.