duanjiao3686 2013-06-20 19:35
浏览 84
已采纳

PHP执行时,Python SSH脚本不起作用

I have a Python (2.7) script that connect to a remote device through SSH and configure it. This script works fine when I run it.

I have a PHP page that execute other Python scripts and they are also working fine. The problem: the SSH script doesn't work when I'm running it from the PHP page. (there is no problem with the PHP page nor the Python script)

To be more specific, the script (when executed by PHP) is running until it gets to the first SSH related code (import paramiko).

How can it be, and is there anything I can do to make it work? Thanks!

(OS: windows XP. Server:Wamp (Apachi http server)

for example, consider this simple code:

fp=open("file.txt","w")
fp.write("text")
import paramiko
fp.write("another text")
fp.close()

if executed by me, everything works fine. If by the PHP page- only the code until "import paramiko" is executed.

or:

fp=open("file.txt","w")
fp.close()
import paramiko
fp2=open("file2.txt","w")
fp2.close()

Executed by me- both files created. by php- only the first.

Moreover: I tried using Try & Catch but nothing comes up. No exception is thrown.

  • 写回答

1条回答 默认 最新

  • dongmieqiao3152 2013-06-20 20:15
    关注

    My guess is that paramiko is not in your PYTHONPATH environmental variable for the Apache user.

    When Apache fires up PHP, it is calling PHP as the whatever the "Apache" user happens to be (in Linux normally apache (or httpd) user in group www-data). Sometimes this user will have different environment variables set than the ones that users normally have. While this generally doesn't matter, sometimes it will cause bizarre interactions (and I can see it happening with Pip).

    I would add a try... except around paramiko:

    try:
       import paramiko
    except Exception as e:
       # log your exception here.
       pass # I put this here so the example can compile.
    #rest of the script
    

    That should let you know whether or not it is installed (and I suspect it isn't).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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