#FTP
1 #socketserver基本使用#服务器端 2 ''' 3 1 你必须创建一个请求处理类。并且这个类要继承BaseRequestHandler, 4 并且还要重写父亲类的handle() 5 6 2 你必须实例化TCPServer,并且传递server ip 和你上面创建的请求处理类 7 给这个TCPServer 8 9 3 调用 Server.handle_request()#只处理一个请求 10 Server.serve_forever()#处理多个请求,并且永远执行 11 12 4 关闭服务 13 ''' 14 15 16 #socketserver基本使用 17 18 import os 19 import json 20 21 import socketserver 22 23 class MyTCPHandler(socketserver.BaseRequestHandler): 24 25 def put(self,*args): 26 #接收客户端文件 27 cmd_dic = args[0] 28 filename = cmd_dic['filename'] 29 filesize = cmd_dic['size'] 30 if os.path.isfile(filename): 31 f = open(filename + '.new','wb') 32 else: 33 f = open(filename ,'wb') 34 35 self.request.send(b'200 ok')#返回json格式 36 received_size = 0 37 while received_size < filesize: 38 data = self.request.recv(1024) 39 f.write(data) 40 received_size += len(data) 41 else: 42 print ('file[%s] has uploaded...' % filename) 43 44 45 def handle(self): 46 while True: 47 try: 48 self.data =self.request.recv(1024).strip() 49 print ('{} wrote:'.format(self.client_address[0])) 50 print (self.data) 51 52 cmd_dic = json.loads(self.data.decode()) 53 action = cmd_dic['action'] 54 55 if hasattr(self,action): 56 func = getattr(self,action) 57 func(cmd_dic) 58 59 #self.request.send(self.data.upper()) 60 except ConnectionResetError as e: 61 print ('err,e') 62 break 63 64 65 66 67 if __name__ == '__main__': 68 69 HOST,PORT = 'localhost',9999 70 #HOST,PORT = '0.0.0.0',9999 71 72 server = socketserver.TCPServer((HOST,PORT),MyTCPHandler) 73 server.serve_forever()
1 #FTP client 2 3 #socketserver基本使用#客户端 4 ''' 5 1 你必须创建一个请求处理类。并且这个类要继承BaseRequestHandler, 6 并且还要重写父亲类的handle() 7 8 2 你必须实例化TCPServer,并且传递server ip 和你上面创建的请求处理类 9 给这个TCPServer 10 11 3 调用 Server.handle_request()#只处理一个请求 12 Server.serve_forever()#处理多个请求,并且永远执行 13 14 4 关闭服务 15 ''' 16 17 18 19 20 import socket 21 import os 22 import json 23 24 class FtpClient(object): 25 def __init__(self): 26 27 self.client = socket.socket() 28 def help(self): #帮助信息 29 msg = ''' 30 ls 31 pwd 32 cd ../.. 33 get filename 34 put filename 35 ''' 36 print (msg) 37 38 def connect(self,ip,port): #连接 39 40 #self.client.connect(('localhost',9999)) 41 42 self.client.connect((ip,port)) 43 44 45 def interactive(self): #交互 46 47 #self.authenticate() #登录判断 48 while True: 49 cmd = input('>>').strip() 50 51 if len(cmd) == 0:continue 52 53 cmd_str = cmd.split()[0] 54 55 if hasattr(self,'cmd_%s'% cmd_str): #反射 56 func = getattr(self,'cmd_%s'%cmd_str) 57 func(cmd) 58 else: 59 self.help() 60 61 def cmd_put(self,*args): 62 cmd_split = args[0].split() 63 if len(cmd_split) > 1 : 64 filename = cmd_split[1] 65 if os.path.isfile(filename): 66 filesize = os.stat(filename).st_size 67 68 #msg_str = '%s|%s' %(filename,filesize)#要考虑长远,不建议使用 69 msg_dic = { 70 'action' : 'put' , 71 'filename' : filename , 72 'size' : filesize , 73 'overridden' : True 74 } 75 76 self.client.send(json.dumps(msg_dic).encode("utf-8")) 77 #防止粘包,等服务器确认(标准请求码404) 78 print ('send',json.dumps(msg_dic).encode("utf-8")) 79 server_response = self.client.recv(1024) 80 f = open (filename,'rb') 81 for line in f: 82 self.client.send(line) 83 84 else: 85 print ('file upload usccess...') 86 87 f.close() 88 89 else: 90 print (filename,'isnot exist') 91 92 def cmd_get(self): 93 pass 94 95 96 ftp = FtpClient() 97 98 ftp.connect('localhost',9999) 99 100 ftp.interactive()
1 # """和'''要区分好 2 3 print('-----------寻找当前环境目录路径--------') 4 import sys,os 5 a = os.path.abspath(__file__) 6 b = os.path.dirname(os.path.abspath(__file__)) 7 c = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 8 d = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 9 10 print(a) 11 print(b) 12 print(c) 13 print(d) 14 15 16 print('--------创建新目录开始-----------') 17 18 19 def mkdir(path): 20 # 引入模块 21 import os 22 23 # 去除首位空格 24 path=path.strip() 25 # 去除尾部 \ 符号 26 path=path.rstrip("\\") 27 28 # 判断路径是否存在 29 # 存在 True 30 # 不存在 False 31 isExists=os.path.exists(path) 32 33 # 判断结果 34 if not isExists: 35 # 如果不存在则创建目录 36 print(path+'创建成功') 37 # 创建目录操作函数 38 os.makedirs(path) 39 return True 40 else: 41 # 如果目录存在则不创建,并提示目录已存在 42 print (path+'目录已存在') 43 return False 44 45 # 定义要创建的目录 46 m = '\\'+input('目录名称:') 47 mkpath = c+m 48 # 调用函数 49 mkdir(mkpath) 50 print('--------创建新目录完成-----------') 51 52 53 print('--------创建 module_alex.py 开始-----------') 54 data1=""" 55 56 #FTP client 57 58 #socketserver基本使用#客户端 59 60 61 import socket 62 import os 63 import json 64 65 class FtpClient(object): 66 def __init__(self): 67 68 self.client = socket.socket() 69 def help(self): #帮助信息 70 71 72 msg = ''' 73 74 ls 75 pwd 76 cd ../.. 77 get filename 78 put filename 79 80 ''' 81 82 83 print (msg) 84 85 def connect(self,ip,port): #连接 86 87 #self.client.connect(('localhost',9999)) 88 89 self.client.connect((ip,port)) 90 91 92 def interactive(self): #交互 93 94 #self.authenticate() #登录判断 95 while True: 96 cmd = input('>>').strip() 97 98 if len(cmd) == 0:continue 99 100 cmd_str = cmd.split()[0] 101 102 if hasattr(self,'cmd_%s'% cmd_str): #反射 103 func = getattr(self,'cmd_%s'%cmd_str) 104 func(cmd) 105 else: 106 self.help() 107 108 def cmd_put(self,*args): 109 cmd_split = args[0].split() 110 if len(cmd_split) > 1 : 111 filename = cmd_split[1] 112 if os.path.isfile(filename): 113 filesize = os.stat(filename).st_size 114 115 #msg_str = '%s|%s' %(filename,filesize)#要考虑长远,不建议使用 116 msg_dic = { 117 'action' : 'put' , 118 'filename' : filename , 119 'size' : filesize , 120 'overridden' : True 121 } 122 123 self.client.send(json.dumps(msg_dic).encode("utf-8")) 124 #防止粘包,等服务器确认(标准请求码404) 125 print ('send',json.dumps(msg_dic).encode("utf-8")) 126 server_response = self.client.recv(1024) 127 f = open (filename,'rb') 128 for line in f: 129 self.client.send(line) 130 131 else: 132 print ('file upload usccess...') 133 134 f.close() 135 136 else: 137 print (filename,'isnot exist') 138 139 def cmd_get(self): 140 pass 141 142 143 ftp = FtpClient() 144 145 ftp.connect('localhost',9999) 146 147 ftp.interactive() 148 149 #socketserver基本使用#客户端 150 151 """ 152 153 154 sys.path.append(d) 155 ls = input('文件名称:') 156 f = open (mkpath + '\\'+ ls + '.py','w+',encoding='utf-8') 157 f.write(data1) 158 159 160 f.flush() 161 f.close() 162 print(f) 163 print('--------创建 module_alex.py 完毕-----------')
您的资助是我最大的动力!
金额随意,欢迎来赏!
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的
因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【颜言】!