• <tfoot id='0ZQgS'></tfoot>
    <i id='0ZQgS'><tr id='0ZQgS'><dt id='0ZQgS'><q id='0ZQgS'><span id='0ZQgS'><b id='0ZQgS'><form id='0ZQgS'><ins id='0ZQgS'></ins><ul id='0ZQgS'></ul><sub id='0ZQgS'></sub></form><legend id='0ZQgS'></legend><bdo id='0ZQgS'><pre id='0ZQgS'><center id='0ZQgS'></center></pre></bdo></b><th id='0ZQgS'></th></span></q></dt></tr></i><div id='0ZQgS'><tfoot id='0ZQgS'></tfoot><dl id='0ZQgS'><fieldset id='0ZQgS'></fieldset></dl></div>
      • <bdo id='0ZQgS'></bdo><ul id='0ZQgS'></ul>

      <small id='0ZQgS'></small><noframes id='0ZQgS'>

    1. <legend id='0ZQgS'><style id='0ZQgS'><dir id='0ZQgS'><q id='0ZQgS'></q></dir></style></legend>

        使用 Python 子进程模块时如何传递变量

        时间:2023-07-21
          • <bdo id='8EckO'></bdo><ul id='8EckO'></ul>
              <tbody id='8EckO'></tbody>

            <small id='8EckO'></small><noframes id='8EckO'>

            • <i id='8EckO'><tr id='8EckO'><dt id='8EckO'><q id='8EckO'><span id='8EckO'><b id='8EckO'><form id='8EckO'><ins id='8EckO'></ins><ul id='8EckO'></ul><sub id='8EckO'></sub></form><legend id='8EckO'></legend><bdo id='8EckO'><pre id='8EckO'><center id='8EckO'></center></pre></bdo></b><th id='8EckO'></th></span></q></dt></tr></i><div id='8EckO'><tfoot id='8EckO'></tfoot><dl id='8EckO'><fieldset id='8EckO'></fieldset></dl></div>

              <tfoot id='8EckO'></tfoot>

                <legend id='8EckO'><style id='8EckO'><dir id='8EckO'><q id='8EckO'></q></dir></style></legend>

                  本文介绍了使用 Python 子进程模块时如何传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 python Subprocess 模块从 python 代码启用/禁用以太网连接.下面是我的代码,其中第一步是寻找可用的以太网连接",下一步根据%interfaces%"中传递的参数启用/禁用以太网连接.

                  I'm trying to use python Subprocess module to enable/disable Ethernet connection from python code. Below is my code in which the first step is looking for the available "Ethernet Connections" and the next step enables/disables the ethernet connection according to the parameter passed in "%interfaces%".

                  for /f "skip=2 tokens=3*" %%A in ('netsh interface show interface') do set interface=%%B
                  
                  netsh interface set interface %interface%  ENABLED
                  

                  现在在 python 中使用时,我无法传递变量,但不确定它是否可能.仅传递如下命令按预期工作:

                  Now when using in python I couldn't pass the variable, not sure if it's even possible though. Passing only command as below works as expected:

                  import subprocess
                  subprocess.call('netsh interface set interface Ethernet10 ENABLED')
                  

                  我想做这样的事情:

                  import subprocess
                  subprocess.call (set x = 'Ethernet0')
                  subprocess.call('netsh interface set interface x  ENABLED')
                  

                  推荐答案

                  subprocess.call 将列表作为参数:

                  subprocess.call(['netsh', 'interface', 'set', 'interface', x, 'ENABLED'])
                  

                  您可以改为传递 shell=True 并且您的字符串会起作用,但这是一个安全风险,因为例如用户可以通过使用 $(command_here)

                  You could instead pass shell=True and your string would work, but it is a security risk, since an user could for example call another command by using $(command_here)

                  如果你仍然想使用字符串,你可以使用 shlex.split.

                  If you still want to use a string, you could use shlex.split.

                  这篇关于使用 Python 子进程模块时如何传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <i id='oIg4L'><tr id='oIg4L'><dt id='oIg4L'><q id='oIg4L'><span id='oIg4L'><b id='oIg4L'><form id='oIg4L'><ins id='oIg4L'></ins><ul id='oIg4L'></ul><sub id='oIg4L'></sub></form><legend id='oIg4L'></legend><bdo id='oIg4L'><pre id='oIg4L'><center id='oIg4L'></center></pre></bdo></b><th id='oIg4L'></th></span></q></dt></tr></i><div id='oIg4L'><tfoot id='oIg4L'></tfoot><dl id='oIg4L'><fieldset id='oIg4L'></fieldset></dl></div>
                      <bdo id='oIg4L'></bdo><ul id='oIg4L'></ul>

                      <legend id='oIg4L'><style id='oIg4L'><dir id='oIg4L'><q id='oIg4L'></q></dir></style></legend>

                      <small id='oIg4L'></small><noframes id='oIg4L'>

                      • <tfoot id='oIg4L'></tfoot>
                            <tbody id='oIg4L'></tbody>