@kaan191, good to hear you are having fun! I have been a bit quiet here lately, which just means I have a ”hot Pythonista project” on that takes up most of my time.
See below for the script I used to communicate with my Pi. I have it configured in Pythonista actions in three different modes:
- Move this file to Pi
- Move this directory to Pi
- Run this file on Pi – move the file, run it, and show the output in Pythonista.
#coding: utf-8
import paramiko
import editor, console
import os.path
import sys
console.clear()
local_path = editor.get_path()
remote_path = os.path.basename(local_path)
local_dir_path = os.path.dirname(local_path)
remote_dir_path = local_dir_path.split('/')[-1]
print('Open SSH connection')
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(<local IP address>, 22, username=<username>, password=<password>, timeout=4)
sftp = s.open_sftp()
fixed_local_root = None
fixed_remote_root = <target dir under root on Pi>
try:
sftp.mkdir(fixed_remote_root)
except:
pass # Ok if already exists
if sys.argv[1] == 'dir':
print('--- Send directory')
#sftp.put(local_dir_path, remote_dir_path)
#parent = os.path.split(local_dir_path)[1]
prefix_len = len(local_dir_path)
for walker in os.walk(local_dir_path):
remote_sftp_path = remote_dir_path + walker[0][prefix_len:]
if remote_sftp_path.endswith('/'):
remote_sftp_path = remote_sftp_path[:-1]
try:
pass
#sftp.mkdir(os.path.join(remotepath, walker[0]))
except:
pass
for file in walker[2]:
print('put', os.path.join(walker[0],file), remote_sftp_path + '/' + file)
#sftp.put(os.path.join(walker[0],file),os.path.join(remotepath,walker[0],file))
else:
print('--- Send file')
sftp.put(local_path, remote_path)
if sys.argv[1] == 'run':
print('--- Run it')
stdin, stdout, stderr = s.exec_command('python3 ' + remote_path)
for line in stdout.readlines():
print(line.rstrip('\n'))
s.close()
print('--- Done')
for line in stderr.readlines():
print('ERROR:', line.rstrip('\n'))
print('--- Complete')