首页 > 解决方案 > 如何在远程 shell 的上下文中进行 pdb 的 stdin/stdout 重定向?

问题描述

我正在编写一个 web shell,在服务器端使用 ptpython、gevent 和 Flask,在客户端使用 xtermjs 和 websockets。

我希望breakpoint()Python 的正常功能能够正常工作,使用pdb.

我很高兴发现Pdb课程需要定制stdinstdout但是我无法让事情正常工作:(

在 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它阻塞。

任何帮助,将不胜感激 !

标签: pythonshellsocketsgevent

解决方案


您必须刷新您的写入(除了猴子补丁):

    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()                   # !!!

推荐阅读