FTP上傳功能

來源:互聯網
上載者:User

標籤:run   pac   bin   pcl   recv   attr   open   unp   for   

##服務端
import socket,struct,json,osclass MYTCPServer: address_family=socket.AF_INET socket_type=socket.SOCK_STREAM allow_reuse_address=False max_packet_size=8192 coding=‘utf-8‘ request_queue_size=5 server_dir=‘file_upload‘ def __init__(self,server_address,bind_and_activate=True): self.server_address=server_address self.socket=socket.socket(self.address_family,self.socket_type) if bind_and_activate: try: self.server_bind() self.server_activate() except: self.server_close() def server_bind(self): if self.allow_reuse_address: self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) self.socket.bind(self.server_address) self.server_address=self.socket.getsockname() # 返回的就是元組(‘127.0.0.1‘, 8080) def server_activate(self): self.socket.listen(self.request_queue_size) def server_close(self): self.socket.close() def get_request(self): return self.socket.accept() def close_request(self,request): request.close() def run(self): while True: self.conn,self.client_addr=self.get_request() print(‘from client‘,self.client_addr) while True: try: head_struct=self.conn.recv(4) if not head_struct:break head_len=struct.unpack(‘i‘,head_struct)[0] head_json=self.conn.recv(head_len).decode(‘utf-8‘) head_dic=json.loads(head_json) print(head_dic) #head_dic={‘cmd‘:‘put‘,‘filename‘:‘a.txt‘,‘filesize‘:123123} cmd=head_dic[‘cmd‘] if hasattr(self,cmd): func=getattr(self,cmd) func(head_dic) except Exception: break self.close_request(self.conn) def put(self,args): file_path=os.path.normpath(os.path.join(self.server_dir,args[‘filename‘])) #os.path.normpath返回該檔案的完整路徑.檔案名稱 filesize=args[‘filesize‘] recv_size=0 print(‘----->‘,file_path) with open(file_path,‘wb‘) as f: while recv_size < filesize: recv_data = self.conn.recv(self.max_packet_size) f.write(recv_data) recv_size+=len(recv_data) print(‘recvsize:%s filesize:%s‘ %(recv_size,filesize))tcpserver1=MYTCPServer((‘127.0.0.1‘,8080))tcpserver1.run()
#os.path.normpath  返回某個檔案的完整路徑及檔案名稱 
#os.path.baaename 返回上述完整路徑的檔案名稱
print(os.path.isfile(‘F:\wangkc\day08\s4.py‘))  #True 
print(os.path.getsize(‘F:\wangkc\day08\s4.py‘)) #2264

#用戶端import socket,struct,json,osclass MYTCPClient:    address_family=socket.AF_INET    socket.type=socket.SOCK_STREAM    allow_reuse_address=False    max_packet_size=8192    coding=‘utf-8‘    request_queue_size=5    def __init(self,server_address,connect=True):        self.server_address=server_address        self.socket=socket.socket(self.address_family,self.socket_type)        if connect:            try:                self.client_connect()            except:                self.client_close()                raise    def client_connect(self):        self.socket.connect(self.server_address)    def client_close(self):        self.socket.close()    def run(self):        while True:            inp=input(‘>>: ‘).strip()            if not inp:continue            l=inp.split()            cmd=l[0]            if hasattr(self,cmd):                func=getattr(self,cmd)                func(l)    def put(self,args):        cmd=args[0]        filename=args[1]        if not os.path.isfile(filename):            print(‘file:%s is not exists‘ % filename)            return        else:            filesize=os.path.getsize(filename)        head_dic={‘cmd‘:cmd,‘filename‘:os.path.basename(filename),‘filesize‘:filesize}        print(head_dic)        head_json=json.dumps(head_dic)        head_json_bytes=bytes(head_json,encoding=self.coding)        head_len_struct=struct.pack(‘i‘,len(head_json_bytes))        self.socket.send(head_len_struct)        self.socket.send(head_json_bytes)        send_size=0        with open(filename,‘rb‘) as f:            for line in f:                self.socket.send(line)                send_size+=len(line)                print(send_size)            else:                print(‘upload successful‘)client=MYTCPClient((‘127.0.0.1‘,8080))client.run()
 


 

FTP上傳功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.