#client
importsocket,os,json,hashlib
classFtpclient(object):
def__init__(self):
self.client=socket.socket()
defhelp(self):
print("put/get/cd/pwd")
defconnect(self,ip,port):
self.client.connect((ip,port))
definteractive(self):
while True:
cmd=input("請輸入要上傳下載的文件:").strip()
iflen(cmd)==0:continue
cmd_str=cmd.split()[0]
ifhasattr(self,cmd_str):
func=getattr(self,cmd_str)
func(cmd)
else:
self.help()
defpwd(self,*args):
while True:
cmd_split = args[0].split()
iflen(cmd_split) >0:
msg_dic = {
"action":"pwd"
}
self.client.send(json.dumps(msg_dic).encode("utf-8"))
cmd_res=self.client.recv(1024) # 收102400字節(jié)數(shù)據(jù)
self.client.send(b"ready recive result")
data=self.client.recv(1024)
print(data.decode())
break
defdir(self,*args):
cmd_split = args[0].split()
iflen(cmd_split)>0:
msg_dic={
"action":"dir"
}
while True:
self.client.send(json.dumps(msg_dic).encode("utf-8"))
cmd_res =self.client.recv(1024) # 收102400字節(jié)數(shù)據(jù)
cmd_res_size=int(cmd_res.decode("utf-8"))
self.client.send(b"ready recive result")
received_size=0
received_data=b''
whilereceived_size
size=1024
else:
size=cmd_res_size - received_size
data=self.client.recv(size)
received_size +=len(data)
received_data +=data
else:
print("cmd_res_size",cmd_res_size)
print(received_data.decode())
break
defput(self,*args):
cmd_split=args[0].split()
iflen(cmd_split)>1:
filename=cmd_split[1]
ifos.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')
forlineinf:
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")
defget(self,*args):
cmd_split=args[0].split()
iflen(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")
whilereceived_size < file_total_size:
iffile_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()
創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、甘肅網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為甘肅等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
#main.py
importsocketserver,json,os,hashlib
classMyTcpHandler(socketserver.BaseRequestHandler):
defput(self,*args):
cmd_dic=args[0]
filename=cmd_dic["filename"]
filesize=cmd_dic["size"]
ifos.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()
whilereceived_size
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)
defget(self,*args):
cmd_dic=args[0]
filename=cmd_dic["filename"]
ifos.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()
forlineinf:
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")
defdir(self,*args):
cmd_dic = args[0]
cmd=cmd_dic["action"]
while True:
res = os.popen(cmd).read()
self.request.send(str(len(res.encode())).encode("utf-8"))
client_ack=self.request.recv(1024)
print("確認客戶端接收信息")
self.request.send(res.encode())
print("執(zhí)行結(jié)果已經(jīng)發(fā)送")
defpwd(self,*args):
cmd_dic = args[0]
cmd=cmd_dic["action"]
while True:
res = os.getcwd()
self.request.send(str(len(res.encode())).encode("utf-8"))
client_ack=self.request.recv(1024)
print("確認客戶端接收信息")
self.request.send(res.encode())
print("執(zhí)行結(jié)果已經(jīng)發(fā)送")
defhandle(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"]
ifhasattr(self,action):
func=(getattr(self,action))
func(cmd_dic)
exceptConnectionResetErrorase:
print("error",e)
break
if__name__=="__main__":
HOST,PORT="localhost",5566
server=socketserver.ThreadingTCPServer((HOST,PORT),MyTcpHandler)
server.serve_forever()
server.allow_reuse_address()