I am trying to build a server based on twisted which will return an SWF embeded in html. However I am unable to achieve this. Following is the HTML code .
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Faro</title>
<style type="text/css" media="screen">
html, body { height:100%; background-color: #ffffff;}
body { margin:0; padding:0; overflow:hidden; }
#flashContent { width:100%; height:100%; }
</style>
</head>
<body>
<div id="flashContent">
<object type="application/x-shockwave-flash" data="Game1.swf" width="860" height="640" id="Game1" style="float: none; vertical-align:middle">
<param name="movie" value="Game1.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="play" value="true" />
<param name="loop" value="true" />
<param name="wmode" value="window" />
<param name="scale" value="showall" />
<param name="menu" value="true" />
<param name="devicefont" value="false" />
<param name="salign" value="" />
<param name="allowScriptAccess" value="sameDomain" />
<a href="http://www.adobe.com/go/getflash">
<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
</a>
</object>
</div>
</body>
</html>
The twisted server code is as follows. I have put an if else in render_GET because the html file is being sent properly once the GET request is sent from the browser with uri (/) . And immediately after that the server is getting another GET request from the browser with uri (/Game1.swf) . However the browser doesn't play the swf file.
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
import time
from pprint import pprint
import os.path
from twisted.python import log
import sys
log.startLogging(sys.stdout)
from swf.movie import SWF
class GameFile(Resource):
content_type = "html"
directory = None
extension = None
isLeaf = True
def render_GET(self, request):
pprint(request.uri)
if(request.uri=='/'):
file = "./tmp/Game1.html"
self.content_type = 'html'
else :
file = "E:/Dev/Server/Local/tmp/Game1.swf"
self.content_type = 'application/x-shockwave-flash'
x = None
request.responseHeaders.setRawHeaders("content-type", [self.content_type])
if ".." in file:
request.setResponseCode(400)
return ""
if not os.path.isfile(file):
request.setResponseCode(404)
print "File not found"
return ""
if(request.uri=='/'):
x = open(file).read()
else:
x = open(file).read()
print x
return x
resource = GameFile()
factory = Site(resource)
reactor.listenTCP(8880, factory)
reactor.run()
On the other hand, if I use a basic server to serve static content from a directory, as explained in twistedmatrix.com, and click on the html file listed on the browser, the swf plays properly. I really appreciate any help in understanding/fixing this issue.
Updates for HTTP POST
gameFile = Resource()
gameFile.putChild(b"", Simple())
class Simple(Resource):
isLeaf = True
def render_POST(self, request):
return File("./tmp/Game1.html")
RESULT
Request did not return bytes
Request:
<Request at 0x14f89948 method=POST uri=/ clientproto=HTTP/1.1>
Resource:<__main__.Simple instance at 0x0000000014F89388>
Value:FilePath('E:\\Dev\\Server\\Local\\tmp\\Game1.html')