标签:内存 end add 返回 byte sock upd result connect
一、实现过程:
步骤一:server 端建立socket对象,并绑定Ip和端口号
步骤二:client建立对象,并与server端连接
步骤三:client发送指令,server接受执行,得到结果并返回给client
核心:一收一发,一收一发,一收一发,重要的事情说三遍
二、知识点总结:
1、server模块常用方法
socket() #建立socket对象 bind() #绑定IP地址,端口号 listen() #监听排队人数 accept() #等待客户机连接,未连接时为阻塞状态 send() \ sendall() #发送bytes类型数据 recv(1024) #接受bytes数据,没有数据进来时,阻塞 close() #关闭当前socket
2、client模块常用方法
socket() #建立socket对象 connect() #与确定的IP,端口号获得连接 send() \ sendall() #发送bytes类型数据 recv(1024) #接受bytes数据,没有数据进来时,阻塞 close() #关闭当前socket
三、示例:
server端代码
1 import socket,subprocess 2 3 sk = socket.socket() 4 address = (‘127.0.0.1‘,8008) 5 sk.bind(address) 6 sk.listen(3) 7 print(‘waiting>>>>>>>>‘) 8 9 while True: 10 conn,addr = sk.accept() 11 print(addr,‘连接成功!‘) 12 while True: 13 try: 14 data = conn.recv(1024) 15 except Exception: 16 break 17 if not data:break 18 print(str(data,‘utf8‘),‘>>>:‘) 19 obj = subprocess.Popen(str(data,‘utf8‘),shell=True,stdout=subprocess.PIPE) 20 cmd_rst = obj.stdout.read() 21 # print(‘cmd_rst type>>>‘,type(cmd_rst),‘>>>‘,cmd_rst) 22 result_len = str(len(cmd_rst)) 23 # print(‘result_len:‘,result_len) 24 conn.sendall(bytes(result_len,‘utf8‘)) 25 print(str(conn.recv(1024),‘utf8‘)) 26 conn.sendall(cmd_rst)
client端代码
1 import socket,subprocess 2 3 sk = socket.socket() 4 address = (‘127.0.0.1‘,8008) 5 sk.connect(address) 6 while True: 7 inp = input(‘>>>请输入指令:‘) 8 if inp == ‘exit‘:break 9 sk.send(bytes(inp,‘utf8‘)) 10 res_len = int(str(sk.recv(1024),‘utf8‘)) 11 sk.send(bytes(‘okokok‘,‘utf8‘)) 12 data = bytes() 13 while len(data) != res_len: 14 rcv = sk.recv(1024) 15 data += rcv 16 print(str(data,‘gbk‘)) 17 sk.close()
执行效果
1 E:\Python36-32\python.exe E:/PycharmProjects/untitled/QICQ/Client.py 2 >>>请输入指令:tasklist 3 4 映像名称 PID 会话名 会话# 内存使用 5 ========================= ======== ================ =========== ============ 6 System Idle Process 0 Services 0 24 K 7 System 4 Services 0 796 K 8 smss.exe 344 Services 0 316 K 9 csrss.exe 476 Services 0 1,544 K 10 wininit.exe 568 Services 0 532 K 11 csrss.exe 580 Console 1 7,240 K 12 winlogon.exe 620 Console 1 648 K 13 services.exe 672 Services 0 3,288 K 14 lsass.exe 688 Services 0 4,444 K 15 lsm.exe 696 Services 0 1,232 K 16 svchost.exe 804 Services 0 3,196 K 17 nvvsvc.exe 872 Services 0 2,200 K 18 svchost.exe 912 Services 0 2,804 K 19 svchost.exe 1008 Services 0 7,156 K 20 svchost.exe 1040 Services 0 47,744 K 21 svchost.exe 1072 Services 0 13,296 K 22 svchost.exe 1200 Services 0 6,000 K 23 Pinyin_2345Svc.exe 1272 Services 0 6,292 K 24 Protect_2345Explorer.exe 1388 Services 0 3,080 K 25 nvxdsync.exe 1416 Console 1 3,200 K 26 nvvsvc.exe 1428 Console 1 1,132 K 27 ZhuDongFangYu.exe 1480 Services 0 5,600 K 28 svchost.exe 1504 Services 0 8,732 K 29 dwm.exe 1660 Console 1 18,628 K 30 explorer.exe 1668 Console 1 51,332 K 31 spoolsv.exe 1852 Services 0 2,188 K 32 svchost.exe 1952 Services 0 3,252 K 33 taskhost.exe 1976 Console 1 4,848 K 34 service.exe 504 Services 0 532 K 35 360sd.exe 2436 Console 1 2,028 K 36 SearchIndexer.exe 2608 Services 0 29,708 K 37 2345PinyinCloud.exe 3160 Console 1 72,176 K 38 360bdoctor.exe 3244 Console 1 1,856 K 39 360rp.exe 3656 Console 1 30,652 K 40 sesvc.exe 1136 Console 1 3,748 K 41 Chrome.exe 3940 Console 1 90,868 K 42 Chrome.exe 1796 Console 1 1,244 K 43 Chrome.exe 1704 Console 1 816 K 44 Chrome.exe 3464 Console 1 21,200 K 45 Chrome.exe 3504 Console 1 150,884 K 46 mscorsvw.exe 1820 Services 0 1,800 K 47 svchost.exe 3312 Services 0 2,104 K 48 Thunder.exe 4916 Console 1 15,800 K 49 Thunder.exe 5156 Console 1 17,056 K 50 Thunder.exe 5332 Console 1 8,416 K 51 DownloadSDKServer.exe 5356 Console 1 13,456 K 52 conhost.exe 5392 Console 1 2,252 K 53 Thunder.exe 6036 Console 1 4,532 K 54 Thunder.exe 6044 Console 1 744 K 55 Thunder.exe 6052 Console 1 744 K 56 Thunder.exe 1212 Console 1 3,248 K 57 xlbrowsershell.exe 924 Console 1 2,460 K 58 svchost.exe 2024 Services 0 5,264 K 59 xlbrowsershell.exe 848 Console 1 880 K 60 xlbrowsershell.exe 4288 Console 1 1,456 K 61 pycharm.exe 2772 Console 1 563,684 K 62 fsnotifier.exe 4032 Console 1 3,284 K 63 conhost.exe 2692 Console 1 2,464 K 64 360sdupd.exe 5800 Console 1 10,620 K 65 WmiPrvSE.exe 1528 Services 0 6,028 K 66 SearchProtocolHost.exe 4608 Services 0 6,828 K 67 SearchFilterHost.exe 4768 Services 0 4,680 K 68 python.exe 6140 Console 1 8,624 K 69 conhost.exe 2364 Console 1 2,980 K 70 python.exe 472 Console 1 8,540 K 71 conhost.exe 2752 Console 1 2,972 K 72 cmd.exe 1056 Console 1 2,548 K 73 tasklist.exe 4512 Console 1 4,764 K
备注:
连续的两次send(),可能出现占包现象,解决办法是在中间加一条recv(),进行一次阻塞
标签:内存 end add 返回 byte sock upd result connect
原文地址:https://www.cnblogs.com/fly10086/p/12345033.html