App Engineサイトがあり、Googleドライブのファイルから最後の300バイトを取得したいのですが、Pythonのurllib2でHTTPRangeヘッダーを次のように使用してみました。
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()))
URLは、実際にはファイルメタデータの「downloadurl」属性です。実行するとこのエラーが発生します。
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
Rangeヘッダーが受け入れられない可能性はありますか?ファイル全体をダウンロードしたくないのですが、解決策を持っている人はいますか?
編集
この答えをくれた@ClaudioCherubinoに感謝します!
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