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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么