J'ai un site App Engine et je veux obtenir les 300 derniers octets d'un fichier dans Google Drive.I essayé d'utiliser l'en-tête Range HTTP avec urllib2 Python comme suit:Google Drive lire le fichier partiel en python
req = urllib2.Request(url)
req.headers['Range'] = 'bytes=%s-%s' % (-300, 2)
f = urllib2.urlopen(req)
# This shows you the *actual* bytes that have been downloaded.
range=f.headers.get('Content-Range')
logging.info("range:"+range)
logging.info("read:"+(f.read()))
l'URL est en fait l'attribut « downloadURL » à partir du fichier metadata.I obtenir cette erreur quand je le lance
File "C:\Python27\lib\urllib2.py", line 527, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 401: Unauthorized
est-il possible que l'en-tête de gamme ne sont pas acceptés? Je ne veux vraiment pas télécharger l'ensemble du fichier. N'importe qui avec une solution?
EDIT
Merci à @Claudio Cherubino pour cette réponse!
creds = GetSessionCredentials() //from Google Python Client API
size = int('size of my file')
headers = {"Range" : 'bytes=%s-%s' % (size-300, size)} //Range Header
http = httplib2.Http()
http = creds.authorize(http) //add OAuth credentials
resp, content = http.request(url, "GET", body=None, headers=headers)
if resp.status == 206:
print 'Status: %s' % resp
print "content:"+content
else:
print 'An error occurred: %s' % resp
Claudio Cherubino merci, je vais ajouter le code dans ma question –