无法从远程python scrip启动RPi中的Flask应用程序

2024-09-29 20:15:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在/home/pi下的src文件夹中运行flask_functions.py应用程序,但我无法调用该特定文件夹中的函数

from paramiko import SSHClient, AutoAddPolicy

def Connect(ip, username='pi', pw='password'): 
    '''ssh into the pi'''
    print('connecting to {}@{}...'.format(username, ip))
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(ip, username=username, password=pw)
    print('connection status =', ssh.get_transport().is_active())
    return ssh

def SendCommand(ssh, command, pw='password'):
    '''send a terminal/bash command to the ssh'ed-into machine '''
    print('sending a command... ', command)
    stdin, stdout, stderr = ssh.exec_command( command )
    if "sudo" in command:
        stdin.write(pw+'\n')
    stdin.flush()
    print('\nstout:',stdout.read())
    print('\nsterr:',stderr.read())

myssh = Connect(ip='10.224.172.138', username='pi', pw='raspberry')
SendCommand(myssh, command='pwd') (prints /home/pi)
SendCommand(myssh, command='cd ~/src & python3 flask_functions.py')
SendCommand(myssh, command='pwd') (prints /home/pi)

以下是我的输出:

connecting to pi@10.224.172.138...
('connection status =', True)
('sending a command... ', 'pwd')
('\nstout:', '/home/pi\n')
('\nsterr:', '')
('sending a command... ', 'cd ~/src & python3 flask_functions.py')
('\nstout:', '')
('\nsterr:', "python3: can't open file 'flask_functions.py': [Errno 2] No such file or directory\n")
('sending a command... ', 'pwd')
('\nstout:', '/home/pi\n')

始终返回根目录,因此我无法正确运行flask应用程序。 如何在特定文件夹中运行脚本?提前谢谢


Tags: pyipflaskhomepiusernamefunctionsssh

热门问题