“导出”的子流程模块错误在Linux上的Python中?

发布于 2024-09-27 08:40:26 字数 718 浏览 2 评论 0原文

我正在设置一个程序将我的计算机连接到我们学校的代理,目前有这样的内容:

import subprocess
import sys

username = 'fergus.barker'
password = '*************'
proxy = 'proxy.det.nsw.edu.au:8080'
options = '%s:%s@%s' % (username, password, proxy)

subprocess.Popen('export http_proxy=' + options)

但运行后我得到:

Traceback (most recent call last):
File "school_proxy_settings.py", line 19, in <module>
 subprocess.Popen('export http_proxy=' + options)
File "/usr/lib/python2.6/subprocess.py", line 621, in __init__
 errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1126, in _execute_child
 raise child_exception
OSError: [Errno 2] No such file or directory

为什么会发生这种情况?

I'm setting up a program to connect my computer to our schools proxy and currently have something like this:

import subprocess
import sys

username = 'fergus.barker'
password = '*************'
proxy = 'proxy.det.nsw.edu.au:8080'
options = '%s:%s@%s' % (username, password, proxy)

subprocess.Popen('export http_proxy=' + options)

But upon running I get:

Traceback (most recent call last):
File "school_proxy_settings.py", line 19, in <module>
 subprocess.Popen('export http_proxy=' + options)
File "/usr/lib/python2.6/subprocess.py", line 621, in __init__
 errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1126, in _execute_child
 raise child_exception
OSError: [Errno 2] No such file or directory

Why is this happening please guys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩人痒 2024-10-04 08:40:26

问题是 export 不是实际的命令或文件。它是 bash 和 sh 等 shell 的内置命令,因此当您尝试 subprocess.Popen 时,您将收到异常,因为它找不到导出命令。默认情况下,Popen 会执行 os.execvp() 来生成新进程,这不允许您使用 shell 内部函数。

您可以执行类似的操作,但必须更改对 Popen 的调用。

http://docs.python.org/library/subprocess.html

您可以指定 < code>shell=True 使其使用 shell 命令。

class subprocess.Popen(args, bufsize=0,executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False,startupinfo=无,creationflags=0)

在 Unix 上,shell=True:如果 args 是字符串,则它指定要通过 shell 执行的命令字符串。这意味着字符串的格式必须与在 shell 提示符下键入时完全相同。例如,这包括引用或反斜杠转义其中包含空格的文件名。如果 args 是一个序列,则第一项指定命令字符串,任何其他项将被视为 shell 本身的附加参数。也就是说,Popen 所做的相当于:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

The problem is that export is not an actual command or file. It is a built-in command to shells like bash and sh, so when you attempt a subprocess.Popen you will get an exception because it can not find the export command. By default Popen does an os.execvp() to spawn a new process, which would not allow you to use shell intrinsics.

You can do something like this, though you have to change your call to Popen.

http://docs.python.org/library/subprocess.html

You can specify shell=True to make it use shell commands.

class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)

On Unix, with shell=True: If args is a string, it specifies the command string to execute through the shell. This means that the string must be formatted exactly as it would be when typed at the shell prompt. This includes, for example, quoting or backslash escaping filenames with spaces in them. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. That is to say, Popen does the equivalent of:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

装纯掩盖桑 2024-10-04 08:40:26

export 不是系统上的单独二进制文件,它实际上只是 shell 本身内的一个命令。例如,尝试在您的系统上使用 which rm。您可能会看到类似以下内容:

[21:16:28] ~ $ which rm
/bin/rm

现在尝试使用 which export。您会得到类似的信息:

[21:16:37] ~ $ which export
/usr/bin/which: no export in (/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:
/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:
/usr/local/sbin:/usr/sbin:/sbin:/home/carter/bin)

因此默认情况下您实际上无法调用 export 流程/子流程。您可能需要查看 os.putenv()< /a> 和 os.environ() 相反。

export is not a separate binary on your system, it is actually just a command within the shell itself. For example, try using which rm on your system. You'll probably see something like:

[21:16:28] ~ $ which rm
/bin/rm

Now try using which export. You'll get something like:

[21:16:37] ~ $ which export
/usr/bin/which: no export in (/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:
/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:
/usr/local/sbin:/usr/sbin:/sbin:/home/carter/bin)

So you can't actually invoke an export process/subprocess by default. You may want to look at os.putenv() and os.environ() instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文