码迷,mamicode.com
首页 > 编程语言 > 详细

Python CMD和粘包问题

时间:2016-08-20 16:03:15      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

1.执行CMD命令

SERVER:

__author__ = Alex
#coding:utf-8
import socket
import subprocess

ip_port = (127.0.0.1,8888)
sk = socket.socket()
sk.bind(ip_port)
sk.listen(5)
print ("服务端启动...")

while True:
    conn,addr = sk.accept()
    while True:
        try:
            recv_data = conn.recv(1024)
        except Exception:
            break
        print (str(recv_data,"utf8"))
        cmd = str(recv_data,"utf8").strip()
        cmd_call = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
        print (type(cmd_call))
        cmd_ret = cmd_call.stdout.read()
        print (type(cmd_ret))
        print (len(cmd_ret))
        if len(cmd_ret) == 0:
            cmd_ret = bno output
        conn.send(cmd_ret)
    conn.close()



CLIENT:

__author__ = Alex
#coding:utf-8
import socket

ip_port = (127.0.0.1,8888)
sk = socket.socket()
sk.connect(ip_port)
print ("客户端启动...")

while True:
    send_data = input(">>>>>").strip()
    if len(send_data) == 0:
        continue
    if send_data == q:
        break
    sk.sendall(bytes(send_data,gbk))
    recv_data = sk.recv(1024)
    print (str(recv_data,"gbk"))
    if send_data =="exit":
        break
sk.close()

 

Python CMD和粘包问题

标签:

原文地址:http://www.cnblogs.com/python-study/p/5790587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!