socket
1 import socket 2 sk = socket. socket() 3 address = ('127.0.0.1',8000) 4 sk. bind( address) 5 sk. listen(3)#设置等待的client数量 6 conn, addr = sk.accept()#获得连接对象,和它的地址 7 while True: 8 date = conn. recv(1024) 9 if not date: #当client端退出后传过来的数据为空,client发送空,sever端不能接收,会处于阻塞状态 10 conn.close() 11 conn,addr = sk.accept() 12 continue 13 print( str( date,'utf8')) 14 inp = input(">>>:") 15 conn. send( bytes( inp)) 16 sk.close ()
1 import socket 2 sk = socket.socket() 3 addr = ('127.0.0.1',8000) 4 sk.connent(addr) 5 while True: 6 inp = input("输入内容") 7 if inp=='exit': 8 break 9 sk.send(bytes(inp)) #发送的内容与接受的内容都是bytes类型 10 11 dete = sk.recv(1024) 12 print(str(date,'utf8')) 13 14 sk.close()
命令的读取:
import subprogress a=subprogress.Popen("dir",shell=True,stdout=subprogress.PIPE)// print(a.stdout.read())
将命令执行后转换过去
粘包现象
1 import socket 2 import subprogress 3 addr=('172.0.0.1',8400) 4 sk=socket.socket() 6 sk. bind(addr) 7 sk.listen() 8 conn,addrs=sk.accept() 9 while True: 10 date= conn.recv() 11 date=subprogress.Popen(str(date,'utf8'),sell=True,stdout=subprogress.PIPE)
1 import socket 2 import subprogress 3 addr=('172.0.0.1',8400) 4 sk=socket.socket() 5 sk. bind(addr) 6 sk.listen(3) 7 conn,addrs=sk.accept() 8 while True: 9 date= conn.recv() 10 date=subprogress.Popen(str(date,'utf8'),sell=True,stdout=subprogress.PIPE) 11 r_date=stdout.read() 12 len=len(r_date) 13 conn.sendall(str(len)) 14 conn.recv(1024)//防止粘包 15 16 17 conn.sendall(date) 18 conn.close() 19 sk.close()
1 import socket 2 sk = socket.socket() 3 addr = ('127.0.0.1',8000) 4 sk.connent(addr) 5 while True: 6 inp = input("输入内容") 7 if inp=='exit': 8 break 9 sk.send(bytes(inp)) #发送的内容与接受的内容都是bytes类型 10 len = sk.recv(1024) 11 leng=0 12 date=bytrs()//用来盛放接受的数据 13 while leng!=int(str(len,'utf8')) 14 15 date+= sk.recv(1024) 16 17 leng +=int(len(str(date,'utf8'))) 18 print(str(date,'utf8')) 19 20 sk.close()
文件的上传与下载:
1 import os 2 import socket 3 sk=socket.socket() 4 addr=('127.0.0.1',8000) 5 sk.connect(addr) 6 7 base_dir=os.path.dirname(os.path.abspath(__file__)) 8 9 while True: 10 inp=input('>>>:').strip()#post| 11.png 11 cmd,path=inp.split("|") 12 path=os.path.join(base_dir,path) 13 fliename=os.path.basename(path) 14 flie_size=os.stat(path).st_size#获取文件的长度,数据在bytes时的长度 15 info="%s|%s|%s|%s".%(cmd,path,filename,file_size) 16 sk.sendall(bytes(info,'utf8')) 17 18 19 f=open(path,"rb") 20 has_send=0 21 while has_send!=file_size: 22 date=f.read(1024)#可以指定读取数据的大小 23 sk.sendall(date) 24 has_send+=len(date)#获取bytes类型时的长度 25 f.close() 26 sk.close() 27 28 29 30 31
1 import os 2 import socket 3 sk=socket.socket() 4 addr=('127.0.0.1',8000) 5 sk.bind(addr) 6 sk.listen(3) 7 conn,add=sk.accept() 8 9 10 base_dir=os.path.dirname(os.path.abspath(__file__)) 11 whiile 1: 12 info=conn.recv(1024) 13 cmd,filename,file_size=info.split("|") 14 path=os.path.join(base_dir,filename) 15 16 f=open(path,"ab") 17 has_recv=0 18 while has_recv!=int(str(file_size,"utf8")): 19 date=conn.recv(1024) 20 f.write(date) 21 has_recv+=len(date) 22 f.close() 23 conn.close()
socket类中方法总结:
sk=socket.socket()//其中的两个参数family:AF_INET() IPV4 AF_INET(6) IPV
AF_UNIX 是UNIX系统下的协议
type:SOCK_STREAM tcp协议 SOCK_DGRAM udp协议
sk.bind(addr)//绑定端口和IP地址
sk.listen( number)//可以等待的数量
sk.accept()//等待client连接并返回client对象,和client的地址
sk.sendall() sk.recv(大小)
sk.setblocking(bool )//设置是否堵塞(默认True)当设为False如果accept()或recv一旦无数据则报错
sk.close()
socketsever:
1 import socketsever 2 3 class Mysever(socketsever.BaseRequestHander): 4 def handle(self)://此方法名不能更改,是重载了父类的方法,是逻辑算法的实现 5 print("sever开始启动,,,”) 6 while True: 7 conn=self.request 8 print(self.client_address) 9 while True: 10 client_date=conn.recv(1024) 11 print(str(client_date,'utf8')) 12 sever_response=input(">>:") 13 conn.sendall(bytes(sever_response,'utf8')) 14 conn.close() 15 if __name__='__main__' 16 sever = socketsever.ThreadingTCPSever(('127.0.0.1,8009'),Mysever)//实例化对象, 17 sever.sever_forever()//执行主逻辑,此函数名不能改变。