Подтвердить что ты не робот

Передача данных в subprocess.check_output

Я хочу вызвать script, передать содержимое строки в свой stdin и извлечь его stdout.

Я не хочу касаться реальной файловой системы, поэтому я не могу создать для нее реальные временные файлы.

используя subprocess.check_output Я могу получить все, что пишет script; как я могу получить входную строку в свой stdin, хотя?

subprocess.check_output([script_name,"-"],stdin="this is some input")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 672, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "/usr/lib/python2.7/subprocess.py", line 1043, in _get_handles
    p2cread = stdin.fileno()
AttributeError: 'str' object has no attribute 'fileno'
4b9b3361

Ответ 1

Используйте Popen.communicate вместо subprocess.check_output.

from subprocess import Popen, PIPE

p = Popen([script_name, "-"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate("this is some input")

Ответ 2

В Python 3.4 и новее вы можете использовать параметр ключевого слова для ввода ввода через STDIN при использовании subprocess.check_output()

Цитата из стандартной документации библиотеки для subprocess.check_output():

Входной аргумент передается на Popen.communicate() и, следовательно, на подпроцессы stdin. Если он используется, он должен быть байтовой последовательностью или строкой, если universal_newlines=True. При использовании внутренний объект Popenавтоматически создается с помощью stdin=PIPE, а аргумент stdin может не также можно использовать.

Пример:

>>> subprocess.check_output(["sed", "-e", "s/foo/bar/"],
...                         input=b"when in the course of fooman events\n")
b'when in the course of barman events\n'
>>> 
>>> # To send and receive strings instead of bytes,
>>> # pass in universal_newlines=True
>>> subprocess.check_output(["sed", "-e", "s/foo/bar/"],
...                         universal_newlines=True,
...                         input="when in the course of fooman events\n")
'when in the course of barman events\n'

Ответ 3

Здесь check_output backported версия для python 2.7 со входом.

from subprocess import (PIPE, Popen, CalledProcessError)

def check_output_input(*popenargs, **kwargs):
    """Run command with arguments and return its output as a byte string.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.

    The arguments are the same as for the Popen constructor.  Example:

    >>> check_output(["ls", "-l", "/dev/null"])
    'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'

    The stdout argument is not allowed as it is used internally.
    To capture standard error in the result, use stderr=STDOUT.

    >>> check_output(["/bin/sh", "-c",
    ...               "ls -l non_existent_file ; exit 0"],
    ...              stderr=STDOUT)
    'ls: non_existent_file: No such file or directory\n'

    There is an additional optional argument, "input", allowing you to
    pass a string to the subprocess stdin.  If you use this argument
    you may not also use the Popen constructor "stdin" argument, as
    it too will be used internally.  Example:

    >>> check_output(["sed", "-e", "s/foo/bar/"],
    ...              input=b"when in the course of fooman events\n")
    b'when in the course of barman events\n'

    If universal_newlines=True is passed, the return value will be a
    string rather than bytes.

    """
    if 'stdout' in kwargs:
        raise ValueError('stdout argument not allowed, it will be overridden.')
    if 'input' in kwargs:
        if 'stdin' in kwargs:
            raise ValueError('stdin and input arguments may not both be used.')
        inputdata = kwargs['input']
        del kwargs['input']
        kwargs['stdin'] = PIPE
    else:
        inputdata = None
    process = Popen(*popenargs, stdout=PIPE, **kwargs)
    try:
        output, unused_err = process.communicate(inputdata)
    except:
        process.kill()
        process.wait()
        raise
    retcode = process.poll()
    if retcode:
        cmd = kwargs.get("args")
        if cmd is None:
            cmd = popenargs[0]
        raise CalledProcessError(retcode, cmd, output=output)
    return output