Python seek on remote file using HTTP

前端 未结 4 1678
遥遥无期
遥遥无期 2020-12-05 08:41

How do I seek to a particular position on a remote (HTTP) file so I can download only that part?

Lets say the bytes on a remote file were: 1234567890

I wanna

4条回答
  •  抹茶落季
    2020-12-05 09:06

    AFAIK, this is not possible using fseek() or similar. You need to use the HTTP Range header to achieve this. This header may or may not be supported by the server, so your mileage may vary.

    import urllib2
    
    myHeaders = {'Range':'bytes=0-9'}
    
    req = urllib2.Request('http://www.promotionalpromos.com/mirrors/gnu/gnu/bash/bash-1.14.3-1.14.4.diff.gz',headers=myHeaders)
    
    partialFile = urllib2.urlopen(req)
    
    s2 = (partialFile.read())
    

    EDIT: This is of course assuming that by remote file you mean a file stored on a HTTP server...

    If the file you want is on an FTP server, FTP only allows to to specify a start offset and not a range. If this is what you want, then the following code should do it (not tested!)

    import ftplib
    fileToRetrieve = 'somefile.zip'
    fromByte = 15
    ftp = ftplib.FTP('ftp.someplace.net')
    outFile = open('partialFile', 'wb')
    ftp.retrbinary('RETR '+ fileToRetrieve, outFile.write, rest=str(fromByte))
    outFile.close()
    

提交回复
热议问题