如何使用 shell = true 使用 Python subprocess.Popen() 将 SIGINT 传递给子

2023-07-21Python开发问题
7

本文介绍了如何使用 shell = true 使用 Python subprocess.Popen() 将 SIGINT 传递给子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我目前正在尝试为 GDB 编写 (Python 2.7.3) 一种包装器,这将允许我从脚本输入动态切换到与 GDB 的交互式通信.

I am currently trying to write (Python 2.7.3) kind of a wrapper for GDB, which will allow me to dynamically switch from scripted input to interactive communication with GDB.

到目前为止我使用

self.process = subprocess.Popen(["gdb vuln"], stdin = subprocess.PIPE,  shell = True)

在我的脚本中启动 gdb.(vuln 是我要检查的二进制文件)

to start gdb within my script. (vuln is the binary I want to examine)

由于 gdb 的一个关键特性是暂停附加进程的执行并允许用户在接收到 SIGINT (STRG+C) 时检查寄存器和内存,因此我确实需要一些方法来将 SIGINT 信号传递给它.

Since a key feature of gdb is to pause the execution of the attached process and allow the user to inspect registers and memory on receiving SIGINT (STRG+C) I do need some way to pass a SIGINT signal to it.

没有

self.process.send_signal(signal.SIGINT)

也没有

os.kill(self.process.pid, signal.SIGINT)

os.killpg(self.process.pid, signal.SIGINT)

为我工作.

当我使用这些功能之一时,没有响应.我想这个问题是由使用 shell=True 引起的.但是,在这一点上,我真的没有想法.这次即使是我的老朋友 Google 也无法真正帮助我,所以也许你可以帮助我.提前致谢.

When I use one of these functions there is no response. I suppose this problem arises from the use of shell=True. However, at this point I am really out of ideas. Even my old friend Google couldn't really help me out this time, so maybe you can help me. Thank's in advance.

干杯,迈克

推荐答案

这对我有用:

import signal
import subprocess

try:
    p = subprocess.Popen(...)
    p.wait()
except KeyboardInterrupt:
    p.send_signal(signal.SIGINT)
    p.wait()

这篇关于如何使用 shell = true 使用 Python subprocess.Popen() 将 SIGINT 传递给子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11