socket实现在python中调用操作系统的命令(subprocess)

在python中调用操作系统的命令
import os
import subprocess
# r = os.popen(‘ipconfig‘)
r = subprocess.Popen(‘ls‘,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)、
# subprocess.Popen(cmd,shell=True,subprocess.stdout,subprocess.stderr)
# cmd : 代表系统命令
# shell = True   代表这条命令是 系统命令,告诉操作系统,将cmd当成系统命令去执行
# stdout   是执行完系统命令之后,用于保存结果的一个管道
# stderr   是执行完系统命令之后,用于保存错误结果的一个管道
stdout = r.stdout.read().decode(‘gbk‘)
stderr = r.stderr.read().decode(‘gbk‘)
print(‘正确的返回结果:‘,stdout)
print(‘错误的返回结果:‘,stderr)
print(‘错误的返回结果:‘,stderr)            #保存变量的方法使得能得到两个错误的信息,否则只能得到一次错误的结果

#客户端发送一条命令用于调用服务器的系统命令
    # 客户端发送要执行命令
    # 服务器执行,执行完将结果返回给客户端
    # 客户端拿到结果呈现到用户眼前
#server层
import socket
import subprocess
sk = socket.socket()
sk.bind((‘127.0.0.1‘,8080))
sk.listen()
conn,addr = sk.accept()
while 1:
    cmd = conn.recv(1024).decode(‘utf-8‘)
    r = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    stdout = r.stdout.read()
    stderr = r.stderr.read()
    if stderr:
        conn.send(stderr)
    else:
        conn.send(stdout)
conn.close()
sk.close()

#client层
import socket
sk = socket.socket()
sk.connect_ex((‘127.0.0.1‘,8080))
while 1:
    cmd = input(‘请输入一个命令>>>‘)
    sk.send(cmd.encode(‘utf-8‘))
    #因为服务器是windows,执行的命令是GBK格式,所以传回来后需要进行解码,解码为GBK格式
    result = sk.recv(102400).decode(‘gbk‘)
    print(result)
sk.close()

原文地址:https://www.cnblogs.com/god-for-speed/p/11719027.html

时间: 2024-08-02 22:51:52

socket实现在python中调用操作系统的命令(subprocess)的相关文章

java中调用操作系统的命令

java.lang.Runtime类提供了exec() 方法来执行操作系统的命令. 使用静态的Runtime.getRuntime()方法可以获得当前的java应用程序对应的Runtime类的实例 Runtime类提供了几个重载的exec()方法,用来支持不同的参数类型,但最终调用的是下边的这个exec()方法: //参数1.命令数组 参数2.环境(和参数)等信息 参数3.命令执行的路径public Process exec(String[] cmdarray, String[] envp, F

python中调用命令行命令

http://blog.csdn.net/pipisorry/article/details/46972171 在Python/wxPython环境下,执行外部命令或者说在Python程序中启动另一个程序的方法 1.os.system(command) os.system()函数用来运行shell命令.此命令可以方便的调用或执行其他脚本和命令 #打开指定的文件 >>>os.system('notepad *.txt') 2.wx.Execute(command, syn=wx.EXEC_

使用ctypes在Python中调用C++动态库

使用ctypes在Python中调用C++动态库 入门操作 使用ctypes库可以直接调用C语言编写的动态库,而如果是调用C++编写的动态库,需要使用extern关键字对动态库的函数进行声明: #include <iostream> using namespace std; extern "C" { void greet() { cout << "hello python" << endl; } } 将上述的C++程序编译成动态链

使用ctype在python中调用c

之前在python中调用c++是通过命令行调用的,参数传递是使用文件IO的形式,所以会特别慢 现在用ctypes,参数传递传的只是内存中的指针,这就很舒服 现在来总结下如何使用cytpes在python中调用c (Ubuntu系统下) 首先写一个test.c的源码 int add(int a, int b) { return a + b; } 然后编译成.so文件 命令如下 gcc -fPIC -c test.c gcc -shared -o test.so test.o 执行完这两个命令之后就

[Python-MATLAB] 在Python中调用MATLAB的API

可以参考官方的说明文档: http://cn.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for-python.html MATLAB Engine API的使用文档: http://cn.mathworks.com/help/matlab/matlab-engine-for-python.html 原材料: 1.MATLAB 2015a  32位的 2.Python 2.7.13    32位

Python脚本传参和Python中调用mysqldump

Python脚本传参和Python中调用mysqldump<pre name="code" class="python">#coding=utf-8 import MySQLdb import sys import os # 李红颖编写,用户湖南CLV数据分割使用 print 'dump database:',sys.argv[1] ##传入的第一个参数,数据库名称 print 'dump table:',sys.argv[2] ##传入的第二个参数,表

如何实现在PHP中调用JAVA

详细说明:http://php.662p.com/thread-275-1-1.html PHP与JAVA JAVA是个非常强大的编程利器,它的扩展库也是非常的有用,这篇教程,主要讲述怎样使用PHP调用功能强大的JAVA 类库(classes).为了方便你的学习,这篇教程将包括JAVA的安装及一些基本的例子. windows下的安装 第一步:安装JDK,这是非常容易的,你只需一路回车的安装好.然后做好以下步骤. 在 Win9x 下加入 :“PATH=%PATH%;C:\jdk1.2.2\bin”

JAVA中调用CMD命令,并输出执行结果

package com.wzw.util; import java.io.BufferedReader; import java.io.InputStreamReader; public class CmdDemo { public static void main(String[] args) { BufferedReader br = null; try { Process p = Runtime.getRuntime().exec("net user"); br = new Bu

python中得到shell命令输出的方法

python中得到shell命令输出的方法: 1. import subprocess output = subprocess.Popen(['ls','-l'],stdout=subprocess.PIPE,shell=True).commun icate() print output[0] 2. import commands return_code, output = commands.getstatusoutput('ls -l') 3. import os process = os.p