dtzk85937 2019-04-07 02:50
浏览 165

来自Python“print(”“)”的SSH2流输出未显示在输出中

I use SSH2 library for my web application and I am trying to get python "print()" results from the code. The problem is when, I connect to SSH2 and do stream_get_contents to get the output into a variable, it doesn't show the python print("") output. There is no output from the python. When I run the script via ssh I get the python print. I need to get the python output into a PHP variable. I've found when I do bash echo "test", it outputs on ssh2. That is what I need but from a python script. Could someone please help as I've been trying to fix this all night.

Executing any bash script with echo works. But when using python output, it doesn't go into the same echo type or whatever.

// PHP API Script, should return with the code from python script

$stream = ssh2_exec($connection, "python -u fetch.py 0000 test");

// Stripped Python Script, I've tried all the possible outputs. It should output like bash "echo test"

#subprocess.call('echo 0',shell=True)
if elems[0].is_displayed():
    print("Game PIN not valid!")
    subprocess.call('echo invalid',shell=True)
    sys.stdout.write("invalid")
    sys.stdout.flush()

else:
    print ("Game PIN is valid!")
    subprocess.call('echo valid',shell=True)
    sys.stdout.write("valid")
    sys.stdout.flush()

I get nothing for output because python or SSH2 isn't keeping the connection alive because the python script is separate from the stream. The SSH2 just executes the script but doesn't return any outputs. Using "echo" returns the result because that is the command to return. I need to get the python output into a PHP variable so I can execute it elsewhere.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记