ssh+ftp+1
Posted whz_it
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh+ftp+1相关的知识,希望对你有一定的参考价值。
bug多多,cd后路径拼接有问题
client
import socket, os, hashlib, json import time, sys class Ftp_client(object): def __init__(self): self.client = socket.socket() def help(self): help_info = ‘‘‘ pwd:查看当前目录 ls:查看目录下文件 cd dir:切换目录 put filename:上传文件 get filename:下载文件 some other command... ‘‘‘ print(help_info) def client_connect(self, ipaddr, port): try: self.client.connect((ipaddr, int(port))) self.inter_action() except socket.gaierror as e: print("连接错误") def cmd_put(self, *args): cmd_spilt = args[0].split() if len(cmd_spilt) == 2: filename = cmd_spilt[1] if os.path.isfile(filename): file_size = os.stat(filename).st_size msg_info = { "action": cmd_spilt[0], "filename": filename, "filesize": file_size, } self.client.send(json.dumps(msg_info).encode("utf-8")) rec_ack = self.client.recv(1024) print(rec_ack.decode("utf-8")) f = open(filename, "rb") for line in f: self.client.send(line) else: f.close() result = self.client.recv(1024) print(result.decode("utf-8")) else: print("文件不存在") else: print("请输入正确的格式put filename") def cmd_get(self, *args): cmd_spilt = args[0].split() if len(cmd_spilt) == 2: filename = cmd_spilt[1] msg_info = { "action": cmd_spilt[0], "filename": filename } self.client.send(json.dumps(msg_info).encode("utf-8")) file_size = self.client.recv(1024) print(file_size.decode("utf-8")) self.client.send(b"ok") recived_size = 0 print("正在下载...") f = open(filename, "wb") while recived_size < int(file_size.decode("utf-8")): data = self.client.recv(1024) recived_size += len(data) f.write(data) else: f.close() print("下载完成") else: print("请输入正确的格式get filename") def os_command(self, *args): os_cmd_spilt = args[0] try: if os_cmd_spilt.startswith("cd"): msg_info = { "action": "cmd_cd", "command": os_cmd_spilt, "dir": pwd_true } elif os_cmd_spilt.startswith("ls"): msg_info = { "action": "cmd_ls", "command": os_cmd_spilt.split()[0] + " " + pwd_true } else: count1 = 0 count2 = 0 for i in os_cmd_spilt.split(): if i.startswith("-"): count1 += 1 else: count2 += 1 print(os_cmd_spilt.split()) msg_info = { "action": "os_command", "command": str(os_cmd_spilt.split()[0:count1 + 1]) + " " + "/" + pwd_true + "/" + str( os_cmd_spilt.split()[-count2 + 1:-1]) } print(msg_info["command"]) print(pwd_true) except NameError as e: msg_info = { "action": "os_command", "command": os_cmd_spilt } self.client.send(json.dumps(msg_info).encode("utf-8")) total_size = self.client.recv(1024) total_size = int(total_size.decode("utf-8")) self.client.send(b"prepared") recived_size = 0 recived_data = b"" while recived_size < total_size: if total_size - recived_size > 1024: size = 1024 else: size = total_size - recived_size data = self.client.recv(size) recived_size += size recived_data += data else: return recived_data.decode("utf-8") def inter_action(self, *args): global pwd_true pwd_true = self.os_command("pwd").strip() while True: cmd_send = input("[[email protected] /%s]#" % pwd_true.strip().split("/")[-1]) if len(cmd_send) == 0: continue elif cmd_send == "help": self.help() continue if hasattr(self, "cmd_%s" % cmd_send.split()[0]): func = getattr(self, "cmd_%s" % cmd_send.split()[0]) func(cmd_send) elif cmd_send.startswith("cd"): if not self.os_command(cmd_send): print("路径不存在") else: pwd_true = self.os_command(cmd_send) elif cmd_send.startswith("pwd"): print(pwd_true) else: print(self.os_command(cmd_send)) if __name__ == "__main__": ftp_client = Ftp_client() ipaddr = input("ip地址:") port = input("端口号:") ftp_client.client_connect(ipaddr, port)
server
import socketserver,os,json,hashlib import time class Ftp_server(socketserver.BaseRequestHandler): def put(self,*args): filename = args[0]["filename"] filesize = args[0]["filesize"] f = open(filename,"wb") recived_size = 0 self.request.send("正在上传中...".encode("utf-8")) while recived_size < filesize: data = self.request.recv(1024) f.write(data) recived_size += len(data) else: self.request.send("传输完成".encode("utf-8")) f.close() def get(self,*args): filename = args[0]["filename"] if os.path.isfile(filename): self.request.send(str(os.stat(filename).st_size).encode("utf-8")) self.request.recv(1024) f = open(filename,"rb") for line in f: self.request.send(line) else: result = self.request.recv(1024) f.close() def os_command(self,*args): cmd_split = args[0] send_msg = os.popen(cmd_split["command"]).read() print(cmd_split["command"]) self.request.send(str(len(send_msg.encode("utf-8"))).encode("utf-8")) self.request.recv(1024) self.request.sendall(send_msg.encode("utf-8")) def cmd_cd(self,*args): cmd_split = args[0] if len(cmd_split["command"].split()) == 2 and cmd_split["command"].startswith("cd") and cmd_split["command"].endswith(".."): send_msg = "/"+cmd_split["dir"].split("/")[-2] elif len(cmd_split["command"].split()) == 2 and cmd_split["command"].startswith("cd") and cmd_split["command"].endswith("."): send_msg = "/"+cmd_split["dir"].split("/")[-1] elif len(cmd_split["command"].split()) == 2 and cmd_split["command"].startswith("cd") and os.path.exists(cmd_split["command"].split()[1]) and not cmd_split["command"].endswith(".") and not cmd_split["command"].endswith(".."): send_msg = cmd_split["command"].split()[1] else: send_msg = os.popen(cmd_split["command"]).read() self.request.send(str(len(send_msg.encode("utf-8"))).encode("utf-8")) self.request.recv(1024) self.request.sendall(send_msg.encode("utf-8")) def cmd_ls(self,*args): cmd_split = args[0] send_msg = os.popen(cmd_split["command"]).read() self.request.send(str(len(send_msg.encode("utf-8"))).encode("utf-8")) self.request.recv(1024) self.request.sendall(send_msg.encode("utf-8")) def handle(self): while True: try: self.data = self.request.recv(1024).strip() print("%s :"%self.client_address[0]) cmd_dict = json.loads(self.data.decode()) cmd = cmd_dict["action"] if hasattr(self,cmd): func = getattr(self,cmd) func(cmd_dict) except ConnectionResetError as e: print(e) break if __name__ == "__main__": HOST,PORT = "localhost",9999 server = socketserver.ThreadingTCPServer((HOST,PORT),Ftp_server) server.serve_forever()
以上是关于ssh+ftp+1的主要内容,如果未能解决你的问题,请参考以下文章