GAE Python : バイトストリーム状の SWF データを html 上に表示
Google App Engine で Python を用いての、バイトストリーム状の SWF データを html 上に表示するためのサンプルです。
処理内で読み込んでいる index.html, sample.swf は main.py と同じディレクトリに配置しているものとします。
main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
URL_ROOT = "/"
URL_SWF = "/swf"
class MainHandler(webapp.RequestHandler):
def get(self):
tmplValues = { "swfUri": URL_SWF }
self.response.out.write(template.render('index.html', tmplValues))
class SwfViewer(webapp.RequestHandler):
def get(self):
from urllib import urlopen
swfBinary = urlopen("sample.swf").read()
self.response.headers["Content-Type"] = "application/x-shockwave-flash"
self.response.headers["Content-Length"] = len(swfBinary)
self.response.out.write(swfBinary)
def main():
application = webapp.WSGIApplication([
(URL_ROOT, MainHandler),
(URL_SWF, SwfViewer)
], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
swf を表示する テンプレート index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<title></title>
<link rel="shortcut icon" href="favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript">
swfobject.embedSWF(
'{{ swfUri }}', 'swf', '240', '240', '4'
);
</script>
</head>
<body>
<div id="swf">
</div>
</body>
</html>
index.html 内では swfobject を用いて swf 表示を行っています。swf の URL を指定する箇所に、SwfViewer クラスを呼び出すための URL パスを指定しています。
今回のサンプルの SwfViewer クラス内では、静的に配置済みの sample.swf を開いてバイトストリームに変換していますが、実際は「データストアから swf データを取得する処理」等に置き換える事となりそうです。
追記)上記サンプルの swf 出力方法では、携帯から swf が表示できない事を確認。設定に何かが足りないのかもしれません。
追記2)Content-Length を設定することで解決しました。


