标签:byte write log .so sock _for highlight bytes listen
server #! bin/usr/evn python # -*- coding:utf-8 -*- import socket sk=socket.socket() sk.bind((‘127.0.0.2‘,888,)) sk.listen(5) while 1: con,addr=sk.accept() con.sendall(bytes(‘welcome‘,encoding=‘utf-8‘)) file_size=str(con.recv(1024),encoding=‘utf-8‘) print(file_size) total_size=int(file_size) has_recv=0 f=open(‘new.jpg‘,‘wb‘) while 1: if total_size==has_recv: break data=con.recv(1024) f.write(data) has_recv+=len(data) f.close()
client
#! bin/usr/evn python # -*- coding:utf-8 -*- import socket,os obj=socket.socket() obj.connect((‘127.0.0.2‘,888,)) ret_byt=obj.recv(1024) ret_str=str(ret_byt,encoding=‘utf-8‘) print(ret_str) size=os.stat(‘004.jpg‘).st_size obj.sendall(bytes(str(size),encoding=‘utf-8‘)) with open(‘004.jpg‘,‘rb‘) as f: for i in f: obj.sendall(i) obj.close()
socketsever 这个可以多线程 #! bin/usr/evn python # -*- coding:utf-8 -*- import socketserver class Myserver(socketserver.BaseRequestHandler): def handle(self):#在handle方法里面写要处理的代码,只能在handle里面写 pass if __name__==‘__main__‘: sever= socketserver.ThreadingTCPServer((‘127.0.0.1‘,888),Myserver) sever.server_forever()
标签:byte write log .so sock _for highlight bytes listen
原文地址:http://www.cnblogs.com/ezway/p/6714170.html