python - 如何在远程 shell 的上下文中进行 pdb 的 stdin/stdout 重定向?
问题描述
我正在编写一个 web shell,在服务器端使用 ptpython、gevent 和 Flask,在客户端使用 xtermjs 和 websockets。
我希望breakpoint()
Python 的正常功能能够正常工作,使用pdb
.
我很高兴发现Pdb
课程需要定制stdin
,stdout
但是我无法让事情正常工作:(
在 web shell 中,用户输入的内容通过 websocket 进入服务器进程:有一个监听 greenlet,它写入由 ptpython 读取的管道。然后,通过 websocket 发送 ptpython 输出。到目前为止一切顺利,效果很好。
现在有了pdb
:感谢sys.breakpointhook
我创建了一个自定义Pdb
实例,其中 stdin 是一个管道,而 stdout 是另一个管道。我小心翼翼地使用gevent.fileobject.FileObjectPosix
非阻塞、协作的 I/O 流。我负责让用户输入写入输入管道,并希望输出到另一个管道,因此我可以将其重定向到我的代码中适当的“标准输出”处理例程。
但是,在收到与pdb
提示对应的第一条消息后,我被卡住了,一切似乎都被阻止了。
我使用以下代码重现了我的行为,任何帮助将不胜感激:
from pdb import Pdb as _Pdb
import gevent
from gevent.fileobject import FileObjectPosix as File
import io
import os
import sys
import socket
debugger = None
class GeventPdb(_Pdb):
def __init__(self, own_stdout):
self._stdout = own_stdout
r1, w1 = os.pipe()
r2, w2 = os.pipe()
self.r = File(r1)
self.w = File(w1, "w")
self.r2 = File(r2)
self.w2 = File(w2, "w")
super().__init__(stdin=self.r, stdout=self.w2)
self.stdout_handling = gevent.spawn(self.handle_stdout)
def send_text(self, txt):
# this should write to the 'stdin' pipe,
# thus provoking input reading from Pdb
print("WRITING TO PDB's STDIN")
self.w.write(txt.decode())
def handle_stdout(self):
# this should read what is written by Pdb
# to Pdb's stdout, to redirect it to our
# own stdout
while True:
print("BEFORE READING PDB's STDOUT")
char = self.r2.read(1)
print(f"WRITING TO CUSTOM STDOUT, stdout len is {len(self._stdout.getvalue())}")
self._stdout.write(char)
def test_func():
debugger.set_trace()
if __name__ == '__main__':
custom_stdout = io.StringIO() # this simulates a custom stdout pipe
debugger = GeventPdb(custom_stdout)
# the next socket is the server socket,
# for 'remote' operation
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 4444))
s.listen(1)
print("Connect client to continue... The client is used to send commands to the debugger")
client, client_addr = s.accept()
def handle_client(client):
while True:
cmd = client.recv(1024)
if not cmd:
break
print("SENDING TEXT TO DEBUGGER...")
debugger.send_text(cmd)
gevent.spawn(handle_client, client)
print("now start a function which starts the debugger...")
print("we should see the pdb prompt")
test_func()
我运行了以下代码,然后通过连接,telnet localhost 4444
然后我可以看到我收到提示,然后我可以键入命令并按 [enter]:的输入管道pdb
上似乎没有收到任何内容
。pdb
它阻塞。
任何帮助,将不胜感激 !
解决方案
您必须刷新您的写入(除了猴子补丁):
def send_text(self, txt):
# this should write to the 'stdin' pipe,
# thus provoking input reading from Pdb
print("WRITING TO PDB's STDIN")
self.w.write(txt.decode())
self.w.flush() # !!!
推荐阅读
- postgresql - 如何在字段行之间返回具有不同值的 ID
- javascript - 音量进度条,OffsetX 未定义
- c++ - “获取”其他获取函数的获取函数
- javascript - 有没有办法访问 marko 类中的“窗口”和“文档”引用?
- arrays - 带有数组的Angular 7 mat-table
- python - 如何使用python apriori解析房屋投票二进制数据集?
- javascript - window.open 运行后刷新多个窗口的 Javascript
- google-chrome - A4 尺寸的 SVG 不能在打印中填满整个 A4 纸
- gatsby - 如何从 Gatsbyjs 中另一个节点上的 url 字段下载图像,以便我可以使用 gatsby-image?
- excel - Excel VBA:将动态变量分配给范围函数的问题