# class Foo(object): # def __init__(self,name): # self.name=name # # f=Foo("song") # print(type(f)) # print(type(Foo)) def func(self): print('heello song %s'%self.name) #类的特殊创建方式 def __init__(self,name,age): self.name=name self.age=age Foo=type('Foo',(object),{'talk':func,'__init__':__init__}) f=Foo("chrn",22) f.talk() print(type(Foo)) ------------------------------- __meteaclass call>new>init ------------------------反射------- class Dog(object): def __init__(self,name): self.name=name def eat(self,food): print("%s is eating ..."%self.name,food) d=Dog("niuhnyna") choice=input(">>:").strip() if hasattr(d,choice): #判断一个obj里是否有指定str的映射 func=getattr(d,choice) 根据字符串去获取obj对象里的方法 func("chenrnghua") ---------------------------------------- class Dog(object): def __init__(self,name): self.name=name def eat(self,food): print("%s is eating ..."%self.name,food) def bulk(self): print("%s is eating ..."%self.name) d=Dog("niuhnyna") choice=input(">>:").strip() if hasattr(d,choice): getattr(d,choice) else: setattr(d,choice,22) v=getattr(d,choice) #set后需要get print(v) # setattr(d,choice,bulk) # func=getattr(d,choice) # func(d) ---------异常处理------------------- data={} try: data['name'] except KeyError as e: print("没有这个key",e) and so on --------------------socket-------------- tcp/ip 三次握手 四次断开 @server import socket server=socket.socket() #绑定要监听的端口 server.bind(('localhost',6969)) server.listen() print("watting...") #conn就是客户端过来 在服务器中生成的连接实例 while True: conn, addr = server.accept() # 等信息 print(conn, addr) print("connected") while True: data=conn.recv(1024) print("recv:",data) if not data: print("client has lost") break conn.send(data.upper()) server.close() ------@client import socket client=socket.socket() #声明类型 同时生成对象 client.connect(('localhost',6969)) while True: msg = input(">>:").strip() client.send(msg.encode("utf-8")) data=client.recv(1024) print("recv:",data.decode()) client.close() -------------------ssh plus------------- import socket,os server=socket.socket() server.bind(('localhost',9998)) server.listen() while True: conn,addr=server.accept() print("new conn:",addr) while True: print("等待新指令") data=conn.recv(1024) #8192k if not data: print("客户端已断开") break print("执行指令,,,",data) cmd_res=os.popen(data.decode()).read() print("before send",len(cmd_res)) if len(cmd_res)==0: cmd_res="cmd has no output.." conn.send(str(len(cmd_res.encode())).encode("utf-8")) #先收文件大小 conn.send(cmd_res.encode("utf-8")) server.close() ------------------------------------ import socket client=socket.socket() client.connect(('localhost',9998)) while True: cmd=input(">>:").strip() if len(cmd)==0: continue client.send(cmd.encode("utf-8")) cmd_res_size = client.recv(1024) #jieshoucmd_len print("命令结果大小:",cmd_res_size ) received_size=0 received_data = b'' while received_size<int(cmd_res_size.decode()): data=client.recv(1024) received_size+=len(data) #有可能小于1024 # print(data.decode()) received_data+=data else: print("cmd res receivs done",received_size) print(received_data.decode()) # cmd_res=client.recv(1024) # print(cmd_res.decode()) client.close() ---------------------------粘包问题--------- @server_ftp import socket,os,hashlib server=socket.socket() server.bind(('localhost',9988)) server.listen() while True: conn,addr=server.accept() print("new conn:",addr) while True: print("等待新指令") data=conn.recv(1024) #8192k if not data: print("客户端已断开") break cmd,filename=data.decode().split() print(filename) if os.path.isfile(filename): f=open(filename,"rb") #生成MD5 m=hashlib.md5() file_size=os.stat(filename).st_size conn.send(str(file_size).encode()) #send file size conn.recv(1024) #wait for ack for line in f: m.update(line) conn.send(line) print("file md5",m.hexgdigest()) f.close() conn.send(m.hexgdigest().encode()) #send to client print("send done") server.close() @client_ftp import socket,hashlib client=socket.socket() client.connect(('localhost',9988)) while True: cmd=input(">>:").strip() if len(cmd)==0: continue if cmd.startswith("get"): client.send(cmd.encode()) server_respone=client.recv(1024) print("server response:" ,server_respone) client.send(b"ready to recv file") file_total_size=int(server_respone.decode()) received_size=0 filename=cmd.split()[1] f=open(filename+".new","wb") # 生成MD5 m = hashlib.md5() while received_size < file_total_size: #解决粘包升级版 if file_total_size-received_size>1024: size=1024 else:#最后一次,剩多少收多少 size=file_total_size-received_size print("last receive:",size) data=client.recv(size) received_size+=len(data) m.update(data) f.write(data) #print(file_total_size,received_size) else: new_file_md5=m.hexdigest() print("file recv done",file_total_size,received_size) f.close() serve_file_md5=client.recv(1024) print("server file md5:",serve_file_md5) print("server file md5:", new_file_md5) client.close() ----------------------------------------------------
python_socket ssh+ftp
最新推荐文章于 2024-05-27 14:50:58 发布