编程语言
首页 > 编程语言> > Python网络之黏包

Python网络之黏包

作者:互联网

 

基础示例

# 服务端
from socket import *
sk = socket()
sk.bind(('127.0.0.1',8090))
sk.listen()
conn,addr = sk.accept()
while True:
    cmd = input('>>>')
    if cmd == 'q':
        conn.send(cmd.encode('utf-8'))
        break
    conn.send(cmd.encode('utf-8'))
    print(conn.recv(1024).decode('gbk'))
conn.close()
sk.close()

# 客户端
from socket import *
from subprocess import *
ck = socket()
ck.connect(('127.0.0.1',8090))
while True:
    cmd = ck.recv(1024).decode('utf-8')
    if cmd == 'q':break
    ret = Popen(cmd,shell=True,
                 stdout=PIPE,
                 stderr=PIPE)
    ck.send(ret.stdout.read())
    ck.send(ret.stderr.read())
ck.close()
C/S远程命令

 

标签:ck,socket,Python,黏包,cmd,网络,send,sk,conn
来源: https://www.cnblogs.com/guge-94/p/10729463.html