【问题标题】:How to pipe variables into popen command with Python如何使用 Python 将变量传递到 popen 命令中
【发布时间】:2020-04-07 00:44:28
【问题描述】:

我当前的问题是我正在使用 python 和 HTML 创建一个应用程序,以使用 HTML 表单来收集数据,然后将这些数据转换为 Python 中的变量,然后我使用子进程 POPEN 将 Azure CLI 命令发送到 azure 租户。当我对命令使用以下代码时,它会返回错误。

Python 代码

@app.route('/storageaccountcreate', methods = ['POST'])
def storageaccountcreate():
    name = request.form['storageaccountname']
    resourcegroup = request.form['resourcegroup']
    subscription = request.form['subscription']
    location = request.form['location']
    sku = request.form['sku']
    cmd = f"az storage account create -n {name} -g {resourcegroup} --subscription {subscription} -l {location} --sku {sku}"

    #This is where the command is initiated using subprocess
    command = Popen(cmd)
    text = command.stdout.read().decode("ascii") 
    print(text)
    with open("file.txt","w") as f:
        f.write(text)
    return (cmd)

错误

[2020-04-06 19:36:46,828] ERROR in app: Exception on /storageaccountcreate [POST]
Traceback (most recent call last):
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "c:\Users\PP284QZ\Desktop\repo\ms-identity-python-webapp-master\app.py", line 67, in storageaccountcreate
    command = Popen(cmd)
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\PP284QZ\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
  File "c:\Users\PP284QZ\.vscode\extensions\ms-python.python-2020.3.71659\pythonFiles\lib\python\debugpy\no_wheels\debugpy\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 631, in new_CreateProcess
    return getattr(_subprocess, original_name)(app_name, cmd_line, *args)
FileNotFoundError: [WinError 2] The system cannot find the file specified

我在这里做错了什么?

【问题讨论】:

    标签: python bash azure subprocess command-line-interface


    【解决方案1】:

    您可以像下面这样更改Popen 的代码:

    command = Popen(cmd, shell=True, stdout=PIPE)
    out, err = command.communicate()
    text = out.decode("UTF-8")
    

    【讨论】:

      猜你喜欢
      • 2012-06-24
      • 1970-01-01
      • 2011-05-16
      • 2019-10-19
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 2012-10-01
      • 2021-11-30
      相关资源
      最近更新 更多