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远程命令

 

上一篇:a*b%p


下一篇:AVR单片机熔丝位问题