0

I've recently come across the functionality or the requests package of python (http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow) that allows to defer downloading the response body until you access the Response.content of a file, as told here : https://stackoverflow.com/a/16696317/8376187

def download_file(url):
    local_filename = url.split('/')[-1]
    # NOTE the stream=True parameter
    r = requests.get(url, stream=True)
    with open(local_filename, 'wb') as f:
        for chunk in r.iter_content(chunk_size=1024):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)
    return local_filename

I use this to stream videos and since the headers of the file is present, my video player read the video smoothly.

I would like to do the same with an SSH/SFTP file transfer, i have tryied to use paramiko for that, but my code reads the file without getting the indexes and headers of the file making my video player fail and and is also very slow.

The code (assuming connected paramiko SSHClient() ) :

sftp_client = client.open_sftp()
remote_file = sftp_client.open('remotefile')
with open('localfile', 'wb') as f:
    try:
        data = remote_file.read(1024)
        while (data):
            f.write(data)
            data = remote_file.read(1024)
    finally:
        remote_file.close()

Is there a way to reproduce the behavior of requests' "stream=True" option with an ssh/sftp transfer in python ?

Thanks :)

JeanMi
  • 290
  • 2
  • 8
  • There are no headers in FTP nor SFTP. Headers are HTTP-only thing. - This might be [XY problem](https://meta.stackexchange.com/q/66377/218578). What do you want to achieve? What do headers have to do with your player being slow or fail? Actually is it slow or does it fail? You cannot get both. – Martin Prikryl Jan 31 '18 at 11:23
  • I'm trying to achieve streaming over sftp with vlc by just reading a file (that way I can download the file while watching it), BTW the native sftp support of VLC does not work for me. By headers I mean that VLC says that there are no indexes in the file and ask to reconstruct, if I do reconstruct the video start but it is downloading super slowly compared to http. – JeanMi Jan 31 '18 at 17:25
  • Show us your code with "requests package", so that we can see what you are actually doing there. – Martin Prikryl Feb 01 '18 at 04:17
  • i eddited my message with that piece of code – JeanMi Feb 01 '18 at 07:10
  • So do I understand right that your code with "requests package" writes not only file contents, but also HTTP headers to the local file? – Martin Prikryl Feb 01 '18 at 07:15
  • If this is correct : http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow, than yes – JeanMi Feb 01 '18 at 07:35
  • Come on, how *"If this is correct ... then yes"*??? You have to know, why don't you check the file??? – Martin Prikryl Feb 01 '18 at 08:32
  • ok, there is something weird there, the http header is ofc not in the local file, but somehow VLC is able to read a file being written by the request package whereas says that there is a broken index with the sftp one. – JeanMi Feb 01 '18 at 08:48
  • So check what's different, when you download the **same file** over "requests" and sftp. – Martin Prikryl Feb 01 '18 at 09:36

0 Answers0