当前位置: 首页>>代码示例>>Python>>正文


Python CPopen.communicate方法代码示例

本文整理汇总了Python中cpopen.CPopen.communicate方法的典型用法代码示例。如果您正苦于以下问题:Python CPopen.communicate方法的具体用法?Python CPopen.communicate怎么用?Python CPopen.communicate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cpopen.CPopen的用法示例。


在下文中一共展示了CPopen.communicate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: execCmd

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
def execCmd(command, sudo=False, cwd=None, data=None, raw=False,
            printable=None, env=None, sync=True, nice=None, ioclass=None,
            ioclassdata=None, setsid=False, execCmdLogger=logging.root,
            deathSignal=0, childUmask=None):
    """
    Executes an external command, optionally via sudo.

    IMPORTANT NOTE: the new process would receive `deathSignal` when the
    controlling thread dies, which may not be what you intended: if you create
    a temporary thread, spawn a sync=False sub-process, and have the thread
    finish, the new subprocess would die immediately.
    """

    if ioclass is not None:
        command = cmdutils.ionice(command, ioclass=ioclass,
                                  ioclassdata=ioclassdata)

    if nice is not None:
        command = cmdutils.nice(command, nice=nice)

    if setsid:
        command = cmdutils.setsid(command)

    if sudo:
        command = cmdutils.sudo(command)

    # Unsubscriptable objects (e.g. generators) need conversion
    if not callable(getattr(command, '__getitem__', None)):
        command = tuple(command)

    if not printable:
        printable = command

    execCmdLogger.debug("%s (cwd %s)", _list2cmdline(printable), cwd)

    p = CPopen(command, close_fds=True, cwd=cwd, env=env,
               deathSignal=deathSignal, childUmask=childUmask)
    if not sync:
        p = AsyncProc(p)
        if data is not None:
            p.stdin.write(data)
            p.stdin.flush()

        return p

    (out, err) = p.communicate(data)

    if out is None:
        # Prevent splitlines() from barfing later on
        out = ""

    execCmdLogger.debug("%s: <err> = %r; <rc> = %d",
                        "SUCCESS" if p.returncode == 0 else "FAILED",
                        err, p.returncode)

    if not raw:
        out = out.splitlines(False)
        err = err.splitlines(False)

    return p.returncode, out, err
开发者ID:kripper,项目名称:vdsm,代码行数:62,代码来源:utils.py

示例2: testEcho

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
 def testEcho(self):
     data = "Hello"
     p = CPopen([EXT_ECHO, "-n", data])
     out, err = p.communicate()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     self.assertEquals(out, data)
开发者ID:oVirt,项目名称:cpopen,代码行数:9,代码来源:tests.py

示例3: execCmd

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
def execCmd(command, sudo=False, cwd=None, data=None, raw=False, logErr=True,
            printable=None, env=None, sync=True, nice=None, ioclass=None,
            ioclassdata=None, setsid=False, execCmdLogger=logging.root,
            deathSignal=0, childUmask=None):
    """
    Executes an external command, optionally via sudo.

    IMPORTANT NOTE: the new process would receive `deathSignal` when the
    controlling thread dies, which may not be what you intended: if you create
    a temporary thread, spawn a sync=False sub-process, and have the thread
    finish, the new subprocess would die immediately.
    """
    if ioclass is not None:
        cmd = command
        command = [ioniceCmdPath.cmd, '-c', str(ioclass)]
        if ioclassdata is not None:
            command.extend(("-n", str(ioclassdata)))

        command = command + cmd

    if nice is not None:
        command = [niceCmdPath.cmd, '-n', str(nice)] + command

    if setsid:
        command = [setsidCmdPath.cmd] + command

    if sudo:
        command = [sudoCmdPath.cmd, SUDO_NON_INTERACTIVE_FLAG] + command

    if not printable:
        printable = command

    cmdline = repr(subprocess.list2cmdline(printable))
    execCmdLogger.debug("%s (cwd %s)", cmdline, cwd)

    p = CPopen(command, close_fds=True, cwd=cwd, env=env,
               deathSignal=deathSignal, childUmask=childUmask)
    p = AsyncProc(p)
    if not sync:
        if data is not None:
            p.stdin.write(data)
            p.stdin.flush()

        return p

    (out, err) = p.communicate(data)

    if out is None:
        # Prevent splitlines() from barfing later on
        out = ""

    execCmdLogger.debug("%s: <err> = %s; <rc> = %d",
                        {True: "SUCCESS", False: "FAILED"}[p.returncode == 0],
                        repr(err), p.returncode)

    if not raw:
        out = out.splitlines(False)
        err = err.splitlines(False)

    return (p.returncode, out, err)
开发者ID:gluster,项目名称:gluster-nagios-common,代码行数:62,代码来源:utils.py

示例4: testCat

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
 def testCat(self):
     path = "/etc/passwd"
     p = CPopen(["cat", path])
     out, err = p.communicate()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     with open(path, "r") as f:
         self.assertEquals(out, f.read())
开发者ID:oVirt,项目名称:cpopen,代码行数:10,代码来源:tests.py

示例5: testStdin

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
    def testStdin(self):
        data = "Hello World"
        p = CPopen(["cat"])
        out, err = p.communicate(data)
        self.assertTrue(p.returncode == 0,
                        "Process failed: %s" % os.strerror(p.returncode))

        self.assertEquals(out, data)
开发者ID:oVirt,项目名称:cpopen,代码行数:10,代码来源:tests.py

示例6: testCwd

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
 def testCwd(self):
     cwd = "/proc"
     p = CPopen(["python", "-c", "import os; print os.getcwd()"],
                cwd=cwd)
     out, err = p.communicate()
     self.assertTrue(p.returncode == 0,
                     "Process failed: %s" % os.strerror(p.returncode))
     self.assertEquals(out.strip(), cwd)
开发者ID:oVirt,项目名称:cpopen,代码行数:10,代码来源:tests.py

示例7: execCmd

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
def execCmd(command, sudo=False, cwd=None, data=None, raw=False, logErr=True,
            printable=None, env=None, sync=True, nice=None, ioclass=None,
            ioclassdata=None, setsid=False, execCmdLogger=logging.root,
            deathSignal=0):
    """
    Executes an external command, optionally via sudo.

    IMPORTANT NOTE: don't define a deathSignal when sync=False
    """
    if ioclass is not None:
        cmd = command
        command = [constants.EXT_IONICE, '-c', str(ioclass)]
        if ioclassdata is not None:
            command.extend(("-n", str(ioclassdata)))

        command = command + cmd

    if nice is not None:
        command = [constants.EXT_NICE, '-n', str(nice)] + command

    if setsid:
        command = [constants.EXT_SETSID] + command

    if sudo:
        command = [constants.EXT_SUDO, SUDO_NON_INTERACTIVE_FLAG] + command

    if not printable:
        printable = command

    cmdline = repr(subprocess.list2cmdline(printable))
    execCmdLogger.debug("%s (cwd %s)", cmdline, cwd)

    p = CPopen(command, close_fds=True, cwd=cwd, env=env,
               deathSignal=deathSignal)
    p = AsyncProc(p)
    if not sync:
        if data is not None:
            p.stdin.write(data)
            p.stdin.flush()

        return p

    (out, err) = p.communicate(data)

    if out is None:
        # Prevent splitlines() from barfing later on
        out = ""

    execCmdLogger.debug("%s: <err> = %s; <rc> = %d",
                        {True: "SUCCESS", False: "FAILED"}[p.returncode == 0],
                        repr(err), p.returncode)

    if not raw:
        out = out.splitlines(False)
        err = err.splitlines(False)

    return (p.returncode, out, err)
开发者ID:doronunu,项目名称:vdsm,代码行数:59,代码来源:utils.py

示例8: testUnicodeArg

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
    def testUnicodeArg(self):
        data = u'hello'
        cmd = [EXT_ECHO, "-n", data]

        p = CPopen(cmd)
        out, err = p.communicate()
        p2 = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out2, err2 = p2.communicate()
        self.assertEquals(out, out2)
开发者ID:oVirt,项目名称:cpopen,代码行数:12,代码来源:tests.py

示例9: testPipeline

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
 def testPipeline(self):
     # echo -n 'it works!' | cat
     data = "it works!"
     p2 = CPopen(["cat"], stdin=PIPE, stdout=PIPE)
     try:
         p1 = CPopen(["echo", "-n", data], stdin=PIPE, stdout=p2.stdin)
         p1.wait()
     finally:
         out, err = p2.communicate()
     self.assertEqual(data, out)
开发者ID:oVirt,项目名称:cpopen,代码行数:12,代码来源:tests.py

示例10: testCloseOnExecStdin

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
 def testCloseOnExecStdin(self):
     # Unset close-on-exec on stdin fd
     data = "data"
     with tempfile.NamedTemporaryFile() as f:
         f.write(data)
         f.flush()
         set_close_on_exec(f.fileno())
         p = CPopen(["cat", f.name], stdin=f.fileno(), stdout=PIPE)
         out, err = p.communicate()
         self.assertEqual(out, data)
开发者ID:oVirt,项目名称:cpopen,代码行数:12,代码来源:tests.py

示例11: execCmd

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
def execCmd(command, sudo=False, cwd=None, data=None, raw=False,
            printable=None, env=None, sync=True, nice=None, ioclass=None,
            ioclassdata=None, setsid=False, execCmdLogger=logging.root,
            deathSignal=0, childUmask=None, resetCpuAffinity=True):
    """
    Executes an external command, optionally via sudo.

    IMPORTANT NOTE: the new process would receive `deathSignal` when the
    controlling thread dies, which may not be what you intended: if you create
    a temporary thread, spawn a sync=False sub-process, and have the thread
    finish, the new subprocess would die immediately.
    """

    command = cmdutils.wrap_command(command, with_ioclass=ioclass,
                                    ioclassdata=ioclassdata, with_nice=nice,
                                    with_setsid=setsid, with_sudo=sudo,
                                    reset_cpu_affinity=resetCpuAffinity)

    # Unsubscriptable objects (e.g. generators) need conversion
    if not callable(getattr(command, '__getitem__', None)):
        command = tuple(command)

    if not printable:
        printable = command

    execCmdLogger.debug(cmdutils.command_log_line(printable, cwd=cwd))

    p = CPopen(command, close_fds=True, cwd=cwd, env=env,
               deathSignal=deathSignal, childUmask=childUmask)
    if not sync:
        p = AsyncProc(p)
        if data is not None:
            p.stdin.write(data)
            p.stdin.flush()

        return p

    (out, err) = p.communicate(data)

    if out is None:
        # Prevent splitlines() from barfing later on
        out = ""

    execCmdLogger.debug(cmdutils.retcode_log_line(p.returncode, err=err))

    if not raw:
        out = out.splitlines(False)
        err = err.splitlines(False)

    return p.returncode, out, err
开发者ID:mykaul,项目名称:vdsm,代码行数:52,代码来源:commands.py

示例12: testNonASCIIUnicodeArg

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
    def testNonASCIIUnicodeArg(self):
        data = u'\u05e9\u05dc\u05d5\u05dd'
        # If the default encoding is not utf-8 the test *should* fail as non
        # ascii conversion shouldn't work
        if sys.getfilesystemencoding() != "UTF-8":
            raise SkipTest("The default encoding isn't unicode")

        cmd = [EXT_ECHO, "-n", data]

        p = CPopen(cmd)
        out, err = p.communicate()
        p2 = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out2, err2 = p2.communicate()
        self.assertEquals(out, out2)
开发者ID:oVirt,项目名称:cpopen,代码行数:17,代码来源:tests.py

示例13: execCmd

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
def execCmd(command, sudo=False, cwd=None, data=None, raw=False,
            printable=None, env=None, sync=True, nice=None, ioclass=None,
            ioclassdata=None, setsid=False, execCmdLogger=logging.root,
            deathSignal=0, childUmask=None, resetCpuAffinity=True):
    """
    Executes an external command, optionally via sudo.

    IMPORTANT NOTE: the new process would receive `deathSignal` when the
    controlling thread dies, which may not be what you intended: if you create
    a temporary thread, spawn a sync=False sub-process, and have the thread
    finish, the new subprocess would die immediately.
    """

    if ioclass is not None:
        command = cmdutils.ionice(command, ioclass=ioclass,
                                  ioclassdata=ioclassdata)

    if nice is not None:
        command = cmdutils.nice(command, nice=nice)

    if setsid:
        command = cmdutils.setsid(command)

    if sudo:
        command = cmdutils.sudo(command)

    # warning: the order of commands matters. If we add taskset
    # after sudo, we'll need to configure sudoers to allow both
    # 'sudo <command>' and 'sudo taskset <command>', which is
    # impractical. On the other hand, using 'taskset sudo <command>'
    # is much simpler and delivers the same end result.

    if resetCpuAffinity and _USING_CPU_AFFINITY:
        # only VDSM itself should be bound
        command = cmdutils.taskset(command, _ANY_CPU)

    # Unsubscriptable objects (e.g. generators) need conversion
    if not callable(getattr(command, '__getitem__', None)):
        command = tuple(command)

    if not printable:
        printable = command

    execCmdLogger.debug(cmdutils.command_log_line(printable, cwd=cwd))

    p = CPopen(command, close_fds=True, cwd=cwd, env=env,
               deathSignal=deathSignal, childUmask=childUmask)
    if not sync:
        p = AsyncProc(p)
        if data is not None:
            p.stdin.write(data)
            p.stdin.flush()

        return p

    (out, err) = p.communicate(data)

    if out is None:
        # Prevent splitlines() from barfing later on
        out = ""

    execCmdLogger.debug(cmdutils.retcode_log_line(p.returncode, err=err))

    if not raw:
        out = out.splitlines(False)
        err = err.splitlines(False)

    return p.returncode, out, err
开发者ID:germanovm,项目名称:vdsm,代码行数:70,代码来源:utils.py

示例14: testEnvUnicodeValue

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
 def testEnvUnicodeValue(self):
     p = CPopen(["printenv"], env={"key": u"\u05d0"})
     out, err = p.communicate()
     self.assertEqual(out, "key=\xd7\x90\n")
开发者ID:oVirt,项目名称:cpopen,代码行数:6,代码来源:tests.py

示例15: testEnv

# 需要导入模块: from cpopen import CPopen [as 别名]
# 或者: from cpopen.CPopen import communicate [as 别名]
 def testEnv(self):
     p = CPopen(["printenv"], env={"key": "value"})
     out, err = p.communicate()
     self.assertEqual(out, "key=value\n")
开发者ID:oVirt,项目名称:cpopen,代码行数:6,代码来源:tests.py


注:本文中的cpopen.CPopen.communicate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。