Python から BlobStore に BLOB をアップロードしようとしていますが、https://...appspot.comとhttps://...com/では機能しますが、http からまったく同じコードを実行すると機能しません。 BlobStore は 500 エラーを返します。
そうでない場合でも、接続を常に安全にしようとしました。
これが私のコードです:
@staticmethod
def save_to_blobstore(_request, id, file_contents, file_type, file_name = 'fixmeh.png'):
upload_url = blobstore.create_upload_url('/upload-item/blob-key/{}'.format(id))
if upload_url.startswith('http://') and 'localhost.com' not in _request.get_host():
upload_url = 'https' + upload_url[4:]
# ----- CREATING FORMDATA -----
boundary = 'WebKitFormBoundaryE19zNvXGzXaLvS5C'
body = '\r\n'.join([
'--' + boundary,
'Content-Disposition: form-data; name="file"; filename="{}"'.format(file_name),
'Content-Type: {}'.format(file_type),
'',
file_contents,
'',
'--' + boundary + '--',
'',
])
headers = {
'Content-Type' : 'multipart/form-data; boundary={}'.format(boundary),
'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0',
}
logging.critical('blobstore save')
# TODO Why the hell do we receive deadline limit exceeded?
request = urllib2.Request(upload_url)
# ----- FORCING SECURE CONNECTION EXCEPT ON LOCALHOST -----
if 'localhost.com' in _request.get_host():
conn = httplib.HTTPConnection(request.get_host(), timeout = 50)
else:
conn = httplib.HTTPSConnection(request.get_host(), timeout = 50)
conn._follow_redirects = True
logging.info(['upload_url', upload_url ])
logging.info(['get_host', request.get_host() ])
logging.info(['get_selector', request.get_selector() ])
conn.request('POST', request.get_selector(), body, headers)
try:
response = conn.getresponse()
response_body = response.read()
logging.info(response_body)
# TODO we need to have an exception if the response is not 200
except DeadlineExceededError:
logging.exception('Saving blob - DeadlineExceededError')
except:
logging.exception('Saving blob')
HTTP 経由で実行すると、これは BlobStore から返されますが、あまり役に立ちません。https://console.developers.google.com/project/.../logsまたは他の場所にブロブストア エラーを記録する方法はありますか。
<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>500 Server Error</title></head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Server Error</h1>
<h2>The server encountered an error and could not complete your request.
<p>Please retry your last submission.</p>
<p>If the problem persists, please contact the person responsible for the application you're using, or, if you are that person,
<a href="http://code.google.com/appengine/community.html">report</a> your
problem and mention this error message and the query that caused it.</h2>
</body></html>
が http では機能しないのに https では機能する理由を教えてください。(SDK では http で動作することに注意してください - そこには ssl はありません。これは appspot でのみ発生します)