Forum Archive

SCP to remote server

ihf

I would like to use the Share sheet to send a photo to a remote server. The following script is my attempt but clearly my use of appex.get_image is wrong but it also fails with appex.get_filepath. How do I properly specify the file from the sharesheet for the sftp.put? I'd also like it to work if I select multiple photos.

```import paramiko
import appex

hostname = 'ipaddress'
password = 'password'
source = appex.get_image()
dest = 'destfilepath'

username = "username"
port = 22

try:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
a = sftp.put(source, dest)

finally:
t.close()
```

cvp

@ihf try this, not tested

import paramiko
import appex
import os

hostname = 'xxxxx'
password = 'yyyyy'
source = appex.get_images()
dest = 'xxx'

username = "zzzzz"
port = 22

try:
    t = paramiko.Transport((hostname, port))
    t.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(t)
    path = 'temp.jpg'
    i = 0
    for im in source:
        im.save(path, quality=95)
        a = sftp.put(path, dest+str(i)+'.jpg')
        os.remove(path)
        i += 1

finally:
    t.close()

Edit: tested and corrected

Please, in the future, insert our code between two lines of triple back-quotes

ihf

@cvp Thanks very much! I kept thinking there must be a way to get the image using the share sheet without first saving it into Pythonista and then deleting it but I guess not.

cvp

@ihf I only save as a local file due to the sftp put command but perhaps it could be possible to send the image as binary data. To be investigated if needed.

cvp

@ihf putfo seems to allow to put a file-like object

JonB

@ihf see this thread:
https://forum.omz-software.com/topic/3299/get-filenames-for-photos-from-camera-roll/19

It is possible to get a file handle to a video asset. I haven't looked, but I suspect something might exist for photos.

cvp

@ihf use this, without passing via a file:

        b = io.BytesIO()
        im.save(b, format='JPEG', quality=95)
        b.seek(0)
        a = sftp.putfo(b, dest+str(i)+'.jpg')
        #im.save(path, quality=95)
        #a = sftp.put(path, dest+str(i)+'.jpg')
        #os.remove(path)
cvp

@JonB with the share sheet, you don't receive an asset but a PIL image

cvp

@ihf better...if you use

source = appex.get_images_data()

Then this is sufficient because source = array of image in bytes

        b = io.BytesIO(im)
        a = sftp.putfo(b, dest+str(i)+'.jpg')