创客百科

姿势共享,有节操无门槛参与的创客百科,创客动力之源 \ (^_^) /

用户工具

站点工具


note:qwedc:remote_control_saks_windows

使用Python远程控制Pi上的SAKS拓展板(第二部分:Windows端的正式远程)

做完Pi端的准备工作以后,我们就可以开始创建Windows的远程控制了。这里,我使用的是paramiko库,大家需要用pip去安装。

- pip install paramiko 然后就是编写代码了。代码如下:

 import paramiko
 import sys
 def ssh_connect( _host, _username, _password ):
     try:
         _ssh_fd = paramiko.SSHClient()
         _ssh_fd.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
         _ssh_fd.connect( _host, username = _username, password = _password )
     except Exception, e:
         print('Authorization Failed!Please check the username,password or your device is connected to the Internet.')
         exit()
     return _ssh_fd
 def ssh_exec_cmd( _ssh_fd, _cmd ):
     return _ssh_fd.exec_command( _cmd )
 def ssh_close( _ssh_fd ):
     _ssh_fd.close()
 def print_ssh_exec_cmd_return(_ssh_fd,_cmd):
     stdin,stdout,stderr=ssh_exec_cmd(_ssh_fd,_cmd)
     err_list = stderr.readlines()
     if len( err_list ) > 0:
         for err_content in err_list:
             print 'ERROR:' + err_content
         exit()
     for item in stdout:
         print item
 if __name__ == '__main__':
     sshd = ssh_connect( '192.168.1.121', sys.argv[1], sys.argv[2])
     print 'Executing \''+sys.argv[3]+'\' command,remote controlling raspberrypi.'
     if len(sys.argv)==4:
         print_ssh_exec_cmd_return(sshd,'cd Raspberry_pi_study;cd SAKS;cd RemoteControl;python main.py '+sys.argv[3])
     else: 
        print_ssh_exec_cmd_return(sshd,'cd Raspberry_pi_study;cd SAKS;cd RemoteControl;python main.py '+sys.argv[3]+' '+sys.argv[4])
     ssh_close( sshd )

(PS:我的树莓派的IP是固定192.168.1.121,如果你的IP不是这个,请在上方代码中替换。) (PPS:我的Pi端的代码存放在~/Raspberry_pi_study/SAKS/Remotecontrol文件夹里面,叫做main.py,在上方大家都可以找到熟悉的cd命令。如果你不是用的这个目录,可以替换代码。)

我是演示视频

本页面的其他翻译:
note/qwedc/remote_control_saks_windows.txt · 最后更改: 2017/04/14 19:12 由 果果