查看文件大小:字节大小linux
import os size = os.path.getsize('0909.py') print(size)
servershell
import socket soc = socket.socket(socket.AF_INET,socket.SOCK_STREAM) soc.bind(('127.0.0.1',8080)) soc.listen(5) conn,addr = soc.accept() data = conn.recv(1024) print('客户端发送过来的:',data) data = data.upper() conn.send(data) conn.close() soc.close()
clientwindows
import socket # 192.168.11.152 # 192.168.11.126 # 127.0.0.1 soc = socket.socket() soc.connect(('192.168.11.152',8080)) soc.send('我'.encode()) data = soc.recv(1024) print(data) soc.close()
import socket soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) soc.bind(('127.0.0.1', 8080)) soc.listen(5) conn, addr = soc.accept() while True: data = conn.recv(1024) print(data.decode()) conn.close() soc.close()
import socket soc = socket.socket() soc.connect(('127.0.0.1',8080)) while True: in_s = input('请输入要法送的数据:') soc.send(in_s.encode())
import socket soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) soc.bind(('192.168.11.44',8080)) #192.168.11.44 soc.listen(5) print('等待链接') conn, addr = soc.accept() print('客户链接',addr) for i in range(100): data = conn.recv(1024) data = data.decode() print('客户端发送过来的:', data) data1 = input('请输入:') conn.send(data1.encode()) conn.close() soc.close()
import socket soc = socket.socket() soc.connect(('192.168.11.44',8080)) for i in range(100): data1 = input('请输入:') soc.send(data1.encode()) data = soc.recv(1024) print('服务端回复的:', data.decode()) soc.close()
server网络
import socket #生成一个socket对象 soc=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #绑定地址跟端口号 soc.bind(('127.0.0.1',8001)) #监听(半链接池的大小),不是链接数 soc.listen(3) #等着客户端来链接,conn至关于链接通道,addr是客户端的地址 while True: print('等待客户端链接') conn,addr=soc.accept() #卡主,若是没有客户端链接,会一直卡在这,当有链接,才继续往下走 print('有个客户端链接上了',addr) while True: try: #windows若是客户端断开,会报错,加了try #linux若是客户端,断开,不会报错,会收到空, # 全部当data为空时,也break #等待接收,最大收取1024个字节 data=conn.recv(1024) #会卡主,当客户端有数据过来,才会执行 if len(data)==0: #处理linux客户端断开, # 若是在window下这段代码根本不会执行 # (即使是客服端发了空,这个地方也不会走到) break print(data) except Exception: break # 关闭通道 conn.close() # 关闭套接字 soc.close()
client多线程
import socket soc = socket.socket() soc.connect(('127.0.0.1',8080)) while True: in_s = input('请输入要发送的数据:') soc.send(in_s.encode())
serverssh
import socket import subprocess soc=socket.socket(socket.AF_INET,socket.SOCK_STREAM) soc.bind(('127.0.0.1',8001)) soc.listen(3) while True: print('等待客户端链接') conn,addr=soc.accept() print('有个客户端链接上了',addr) while True: try: data=conn.recv(1024) if len(data)==0: break print(data) obj = subprocess.Popen(str(data,encoding='utf-8'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) #执行正确的结果 b 格式,gbk编码(windows平台) msg=obj.stdout.read() #把执行的结果经过网络传给c端 conn.send(msg) except Exception: break # 关闭通道 conn.close() # 关闭套接字 soc.close()
clientsocket
import socket soc=socket.socket() soc.connect(('127.0.0.1',8001)) while True: in_s=input('请输入要执行的命令:') soc.send(in_s.encode('utf-8')) data=soc.recv(1024) print(str(data,encoding='gbk')) #粘包:tcp会把数据量较小,时间间隔较短的数据,当作同一个包发送
补充的subprocess模块tcp
#ssh 是远程执行命令 #subprocess 执行系统命令的模块 import subprocess #执行系统dir命令,把执行的正确结果放到管道中 # obj=subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE) obj=subprocess.Popen('tasklistdd',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) #拿到正确结果的管道,读出里面的内容 ss=obj.stdout.read() err=obj.stderr.read() print('错误信息',str(err,encoding='gbk')) #由于windows用的gbk编码,用gbk解码 # print(str(ss,encoding='utf-8')) print(str(ss,encoding='gbk'))
TCP能够开始多线程编码
server
#使用socketserver写服务端 #导入模块 import socketserver #本身定义一个类,必须继承BaseRequestHandler class MyTcp(socketserver.BaseRequestHandler): #必须重写handle方法 def handle(self): try: while True : #通讯循环 # print(self) #给客户端回消息 #conn对象就是request #接收数据 print(self.client_address) data=self.request.recv(1024) print(data) if len(data)==0: return #发送数据 self.request.send(data.upper()) except Exception: pass if __name__ == '__main__': #实例化获得一个tcp链接的对象,Threading意思是说,只要来了请求,它自动的开线程来处理链接跟交互数据 #第一个参数是绑定的地址,第二个参数传一个类 server=socketserver.ThreadingTCPServer(('127.0.0.1',8009),MyTcp) #一直在监听 #这么理解:只要来一个请求,就起一个线程(造一我的,作交互) server.serve_forever()
client1
import socket soc = socket.socket() soc.connect(('127.0.0.1',8009)) while True: soc.send('xxx'.encode('utf8')) print(soc.recv(1024))
client2
import socket soc = socket.socket() soc.connect(('127.0.0.1',8009)) while True: soc.send('xxx'.encode('utf8')) print(soc.recv(1024))