我在班级中调用变量时遇到问题。我已将所有内容都设置为自己的内容,但我仍然遇到错误。我想我很难弄清楚这一点,因为我是 3.0 脚本的新手。
这是我的脚本:
这是错误:
command = 'tcpdump -c5 -tttt -w {0} host {1}'.format(raw, input_host)
NameError: global name 'raw' is not defined
Run Code Online (Sandbox Code Playgroud)
如果我让它们 self.raw 或 self.input_host
它得到这个:
command = 'tcpdump -c5 -tttt -w {0} host {1}'.format(self.raw, self.input_host)
AttributeError: 'MainLoop' object has no attribute 'raw'
Run Code Online (Sandbox Code Playgroud) 我正在使用CreateProcess来运行我的进程/应用程序。目的是运行它,执行某些操作,等待某些指示,然后关闭它(使用TerminateProcess)。
我注意到这个应用程序/进程创建了子进程。另外,当终止创建的进程时,子进程并不会终止,仍会保留一段时间。
我想问是否有一个选项可以以某种方式杀死主进程的所有子进程。它会导致问题,因为当我再次执行CreateProcess时,会有以前进程的剩余内容,我认为它们会导致一些问题。
我真的很感谢你的帮助!
我试图让一个 linux 二进制文件通过使用子进程将其标准输出发送到一个变量。但只要不断得到回溯。
>>> import subprocess
>>>nmap -sn -Pn todd.ns.cloudflare.com --script dns-check-zone --script-args='dns-check-zone.domain=www.macmonster.com
Run Code Online (Sandbox Code Playgroud)
任何想法(哦,我使用 Python2.7)。
理想情况下,我想避免使用 Shell=true 来避免任何安全问题。
谢谢,
我想从另一个 .py 文件中获取路径名。
我把那个 .py 文件称为
xy=subprocess.check_call(["python","/home/emeks/workspace/ex/ex.py"])
print xy
Run Code Online (Sandbox Code Playgroud)
但是那个打印命令总是打印零 (0) 但我想得到路径名。
我该怎么办
我刚刚找到了这个很棒的 wget 包装器,我想使用 subprocess 模块将其重写为 python 脚本。然而,事实证明这很棘手,给了我各种各样的错误。
download()
{
local url=$1
echo -n " "
wget --progress=dot $url 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
echo -ne "\b\b\b\b"
echo " DONE"
}
Run Code Online (Sandbox Code Playgroud)
然后可以这样调用:
file="patch-2.6.37.gz"
echo -n "Downloading $file:"
download "http://www.kernel.org/pub/linux/kernel/v2.6/$file"
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
来源: http: //fitnr.com/showing-file-download-progress-using-wget.html
我有一个用 python 编写的程序并在其中使用了 git 命令。出于某种原因,我不想使用 git-python 或其他而不是子进程。但我目前被困在获取git clone输出上。
我试过一些代码片段。有些命令可以很好地使用ping 8.8.8.8,但不适用于git clone.
例如
使用线程
def log_worker(stdout):
while True:
last = non_block_read(stdout).strip()
if last != "":
print(last)
def non_block_read(output):
fd = output.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
try:
return output.read()
except:
return ''
def test():
mysql_process = subprocess.Popen(
"ping google.com",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
thread = Thread(target=log_worker, args=[mysql_process.stdout])
thread.daemon = True
thread.start()
mysql_process.wait()
thread.join(timeout=1)
test()
Run Code Online (Sandbox Code Playgroud)
或者
newlines = ['\n', '\r\n', '\r']
def unbuffered(proc, …Run Code Online (Sandbox Code Playgroud) 我想通过 subprocess.Popen 从 python 执行一个外部程序。我想知道是否可以为通过命令执行的外部程序设置窗口的大小和位置?
我正在使用 Gphoto2 在 DSLR 上拍照。由于它基于我尝试使用的 bash 命令,subprocess.communicate但在相机拍照后它会冻结。
如果我gphoto2 --capture-image-and-download在终端中尝试它需要不到 2 秒。我正在研究树莓派。
代码:
import subprocess
class Wrapper(object):
def __init__(self, subprocess):
self._subprocess = subprocess
def call(self,cmd):
p = self._subprocess.Popen(cmd, shell=True, stdout=self._subprocess.PIPE, stderr=self._subprocess.PIPE)
out, err = p.communicate()
return p.returncode, out.rstrip(), err.rstrip()
class Gphoto(Wrapper):
def __init__(self, subprocess):
Wrapper.__init__(self,subprocess)
self._CMD = 'gphoto2'
def captureImageAndDownload(self):
code, out, err = self.call(self._CMD + " --capture-image-and-download")
if code != 0:
raise Exception(err)
filename = None
for line in out.split('\n'):
if line.startswith('Saving file as '):
filename …Run Code Online (Sandbox Code Playgroud) 我试图在 linux 上使用 subprocess.call() 调用 ffmpeg 命令,但我无法正确获取参数。之前,我使用了 os.system 并且它有效,但不推荐这种方法。
使用诸如“-i”之类的带破折号的参数会出现此错误
Unrecognized option 'i "rtsp://192.168.0.253:554/user=XXX&password=XXX&channel=0&stream=0.sdp?real_stream"'.
Error splitting the argument list: Option not found
Run Code Online (Sandbox Code Playgroud)
使用像“i”这样没有破折号的参数会出现这个错误
[NULL @ 0x7680a8b0] Unable to find a suitable output format for 'i rtsp://192.168.0.253:554/user=admin&password=&channel=0&stream=0.sdp?real_stream'
i rtsp://192.168.0.253:554/user=XXX&password=XXX&channel=0&stream=0.sdp?real_stream: Invalid argument
Run Code Online (Sandbox Code Playgroud)
这是代码
class IPCamera(Camera):
"""
IP Camera implementation
"""
def __init__(self,
path='\"rtsp://192.168.0.253:554/'
'user=XXX&password=XXX&channel=0&stream=0.sdp?real_stream\"'):
"""
Constructor
"""
self.path = path
def __ffmpeg(self, nb_frames=1, filename='capture%003.jpg'):
"""
"""
ffm_input = "-i " + self.path
ffm_rate = "-r 5"
ffm_nb_frames = "-vframes " + str(nb_frames) …Run Code Online (Sandbox Code Playgroud) 我有一个脚本,它包含一个名单-名单只是一些ARGS,我想传递给subprocess.run这样的
commands = ["bash command 1", "bash command 2",..]
Run Code Online (Sandbox Code Playgroud)
这是我的代码
commands = ["bash command 1", "bash command 2",..]
process = subprocess.run([commands], stdout = subprocess.PIPE, shell = True)
Run Code Online (Sandbox Code Playgroud)
如何将列表传递给我的 subprocess.run?
这是回溯
Traceback (most recent call last):
File "./retesting.py", line 18, in <module>
process = subprocess.run([commands], stdout = subprocess.PIPE, shell = True)
File "/usr/lib/python3.5/subprocess.py", line 383, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.5/subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1221, in _execute_child
restore_signals, start_new_session, preexec_fn) …Run Code Online (Sandbox Code Playgroud) subprocess ×10
python ×9
bash ×2
communicate ×1
ffmpeg ×1
git ×1
nmap ×1
popen ×1
process ×1
python-3.x ×1
raspberry-pi ×1
shell ×1
winapi ×1
windows ×1
wrapper ×1