码迷,mamicode.com
首页 > 其他好文 > 详细

socket 远程命令

时间:2018-07-15 11:14:44      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:lis   recv   ddr   proc   空值   connect   process   bsp   div   

# -*- coding: utf-8 -*-

from socket import *
import subprocess

server = socket()
server.bind((‘‘,10000))
server.listen(5)

while True:
    conn,addr = server.accept()
    print( ‘客户端ip地址:‘,addr )
    while True:
        try:
            cmd = conn.recv(1024)
            # if not cmd: break       # 如果是
            print( ‘收到命令:‘,cmd )

            # 生成可执行命令
            res  = subprocess.Popen(cmd.decode(‘gbk‘),shell=True,
                                    stderr=subprocess.PIPE,
                                    stdout=subprocess.PIPE,
                                    stdin=subprocess.PIPE)
            err = res.stderr.read()              #  执行命令
            if not err:                         #  命令正确
                cmd_res = err                   #  返回执行结果
            else:
                cmd_res = res.stderr.read()      #  错误命令,再读进是空值
            conn.send(cmd_res)                  #  结果返回客户端
        except Exception as e:
            print(‘111‘,e)
            break
        conn.close()

  

 

# -*- coding: utf-8 -*-

from socket import *

cl = socket()
cl.connect((‘127.0.0.1‘,10000))

while True:
    cmd = input(‘--->>>‘)
    if not cmd:    continue
    if cmd == ‘quit‘: break

    cl.send(cmd.encode(‘gbk‘))
    re = cl.recv(2048)
    print(‘命令执行结果:‘,re.decode(‘gbk‘))

  

socket 远程命令

标签:lis   recv   ddr   proc   空值   connect   process   bsp   div   

原文地址:https://www.cnblogs.com/gdwz922/p/9311762.html

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