omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    SCP to remote server

    Pythonista
    3
    9
    3524
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ihf
      ihf last edited by 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 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 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @ihf last edited by 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

        1 Reply Last reply Reply Quote 0
        • ihf
          ihf last edited by

          @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 JonB 5 Replies Last reply Reply Quote 0
          • cvp
            cvp @ihf last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • cvp
              cvp @ihf last edited by

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

              1 Reply Last reply Reply Quote 0
              • JonB
                JonB @ihf last edited by

                @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 1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @ihf last edited by

                  @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)
                  
                  1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @JonB last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @ihf last edited by

                      @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')
                      
                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post
                      Powered by NodeBB Forums | Contributors