溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

socket_ftp_上傳下載

發(fā)布時(shí)間:2020-10-17 12:38:12 來(lái)源:網(wǎng)絡(luò) 閱讀:317 作者:leiwenbin627 欄目:編程語(yǔ)言

#main.py
import socketserver,json,os,hashlib
class MyTcpHandler(socketserver.BaseRequestHandler):
   
def put(self,*args):
        cmd_dic=args[
0]
        filename=cmd_dic[
"filename"]
        filesize=cmd_dic[
"size"]
       
if os.path.isfile(filename):
            f=
open(filename+".new",'wb')
       
else:
            f=
open(filename,'wb')
       
self.request.send(b'200 ok')
        received_size=
0
       
m=hashlib.md5()
       
while received_size<filesize:
            data=
self.request.recv(1024)
            m.update(data)
            f.write(data)
            received_size+=
len(data)
       
else:
            uploaded_file_md5=m.hexdigest()
           
self.request.send(b"ready recive your md5")
            client_file_md5=
self.request.recv(1024)
            
print("client_file_md5:",client_file_md5.decode())
           
print("uploaded_file_md5:",uploaded_file_md5)
           
print("file [%s] has uploaded..."%filename)
   
def get(self,*args):
        cmd_dic=args[
0]
        filename=cmd_dic[
"filename"]
       
if os.path.isfile(filename):
            f=
open(filename,'rb')
            filesize=os.stat(filename).st_size
           
self.request.send(str(filesize).encode())
           
self.request.recv(1024)
            m = hashlib.md5()
           
for line in f:
                m.update(line)
               
self.request.send(line)
           
else:
               
print("file md5:", m.hexdigest())
               
self.request.recv(1024)
               
self.request.send(m.hexdigest().encode("utf-8"))
               
print("file download  success...")
                f.close()
       
else:
           
print(filename,"is not exist")
   
def handle(self):
       
while True:
           
try:
               
self.data=self.request.recv(1024).strip()
                
print("{} wrote:".format(self.client_address[0]))
               
print(self.data.decode())
                cmd_dic=json.loads(
self.data.decode())
               
print("cmd_dic:",cmd_dic)
                action=cmd_dic[
"action"]
               
if hasattr(self,action):
                    func=(
getattr(self,action))
                    func(cmd_dic)
           
except ConnectionResetError as e:
               
print("error",e)
               
break
if
__name__=="__main__":
    HOST,PORT=
"localhost",5566
   
server=socketserver.ThreadingTCPServer((HOST,PORT),MyTcpHandler)
    server.serve_forever()
    server.allow_reuse_address()

#client.py
import socket,os,json,hashlib
class Ftpclient(object):
   
def __init__(self):
        self.client=socket.socket()
   
def help(self):
       
print("put/get/cd/pwd")
   
def connect(self,ip,port):
        self.client.connect((ip,port))
   
def interactive(self):
       
while True:
            cmd=
input("請(qǐng)輸入要上傳下載的文件:").strip()
           
if len(cmd)==0:continue
           
cmd_str=cmd.split()[0]
           
if hasattr(self,cmd_str):
                func=
getattr(self,cmd_str)
                func(cmd)
           
else:
                self.help()
   
def put(self,*args):
        cmd_split=args[
0].split()
       
if len(cmd_split)>1:
            filename=cmd_split[
1]
           
if os.path.isfile(filename):
                filesize=os.stat(filename).st_size
                msg_dic={
                   
"action":"put",
                   
"filename":filename,
                   
"size":filesize,
                   
"overridden":True
               
}
                self.client.send(json.dumps(msg_dic).encode(
"utf-8"))
               
server_response=self.client.recv(1024)
                m=hashlib.md5()
                f=
open(filename,'rb')
               
for line in f:
                    m.update(line)
                    self.client.send(line)
               
else:
                   
print("file md5:", m.hexdigest())
                    self.client.recv(
1024)
                    self.client.send(m.hexdigest().encode(
"utf-8"))
                   
print("file upload success...")
                    f.close()
           
else:
               
print(filename,"is not exist")
   
def get(self,*args):
        cmd_split=args[
0].split()
       
if len(cmd_split)>1:
            filename=cmd_split[
1]
            msg_dic={
                   
"action":"get",
                   
"filename":filename,
                   
"overridden":True
           
}
            self.client.send(json.dumps(msg_dic).encode(
"utf-8"))
            received_size =
0
           
m = hashlib.md5()
            f=
open(filename+".download",'wb')
            server_response=self.client.recv(
1024)
            file_total_size=
int(server_response.decode())
            self.client.send(
b"ready to recv file")
           
while received_size < file_total_size:
               
if file_total_size - received_size > 1024:
                    size=
1024
               
else:
                    size=file_total_size - received_size
                data = self.client.recv(size)
                m.update(data)
                f.write(data)
                received_size +=
len(data)
           
else:
                download_file_md5 = m.hexdigest()
                self.client.send(
b"ready recive your md5")
                servrer_file_md5 = self.client.recv(
1024)
               
print("server_file_md5:",servrer_file_md5.decode())
               
print("download_file_md5:",download_file_md5)
                
print("file [%s] has download..."%filename)
               
print("源文件和下載文件大小分別為:",received_size,file_total_size)
ftp=Ftpclient()
ftp.connect(
"localhost",5566)
ftp.interactive()
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI