加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

在Linux上的python中使用’export’进行子进程模块错误?

发布时间:2020-12-14 02:21:07 所属栏目:Linux 来源:网络整理
导读:我正在设置一个程序将我的计算机连接到我们的学校代理,目前有这样的东西: import subprocessimport sysusername = 'fergus.barker'password = '*************'proxy = 'proxy.det.nsw.edu.au:8080'options = '%s:%s@%s' % (username,password,proxy)subproc
我正在设置一个程序将我的计算机连接到我们的学校代理,目前有这样的东西:
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

为什么会这样呢?

解决方法

问题是导出不是实际的命令或文件.它是bash和sh之类的shell的内置命令,所以当你尝试subprocess.Popen时,你会得到一个异常,因为它找不到导出命令.默认情况下,Popen执行一个os.execvp()来生成一个新进程,该进程不允许使用shell内部函数.

你可以做这样的事,虽然你必须改变你对Popen的号召.

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

您可以指定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 = 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],...])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读