微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在远程 shell 的上下文中对 pdb 进行 stdin/stdout 重定向?

如何解决如何在远程 shell 的上下文中对 pdb 进行 stdin/stdout 重定向?

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

我希望 Python 的正常 breakpoint() 函数使用 pdb 工作。

我很高兴地发现 Pdb 类采用自定义stdinstdout,但是我无法让它工作:(

在 web shell 中,用户输入的内容通过 websocket 进入服务器进程:有一个监听 greenlet,它写入一个由 ptpython 读取的管道。 然后,ptpython 输出通过 websocket 发送。到目前为止一切顺利,效果很好。

现在有了 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 连接然后我可以看到我收到 pdb 提示,然后我可以输入命令并按 [enter]:似乎没有收到任何信息 pdb 的输入管道。它阻塞。

任何帮助将不胜感激!

解决方法

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

import './App.css';
import React,{ Component } from 'react';
import Clock from './Clock';


class App extends Component {

  state = {
    secondRatio: 0,minuteRatio: 0,hourRatio: 0,fullname: '',}

  componentDidMount() {
    let fullname = prompt("Lutfen isim giriniz:");
    this.setState({ ...this.state,fullname });
    setInterval(() => (
      this.setClock()
    ),1000);
    console.log(this.state.fullname);
  }

  setClock = () => {
    const currentDate = new Date;
    let secondRatio = currentDate.getSeconds() / 60;
    let minuteRatio = (secondRatio + currentDate.getMinutes()) / 60;
    let hourRatio = (minuteRatio + currentDate.getHours()) / 12;
    this.setState({ secondRatio: secondRatio });
    this.setState({ minuteRatio: minuteRatio });
    this.setState({ hourRatio: hourRatio });
  }

  render() {

    const { secondRatio,minuteRatio,hourRatio } = this.state;

    return (
      <fragment>
        <div>  <h1 className="text1">Hello,{this.state.fullname} ! Welcome! </h1>
          <div className="test"><Clock secondRatio={secondRatio} minuteRatio={minuteRatio} hourRatio={hourRatio} />
          </div>
        </div>
      </fragment>
    );
  }
}

export default App;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。