Skip to content

Instantly share code, notes, and snippets.

@progrium
Created December 7, 2009 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save progrium/250676 to your computer and use it in GitHub Desktop.
Save progrium/250676 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from __future__ import with_statement
import base64
import PyV8
import time
from twisted.application import service, internet
from twisted.web import server, resource, client
from urllib import unquote
import simplejson
sessions = {}
class ContextResource(resource.Resource):
isLeaf = True
def render_POST(self, request):
if request.path in sessions:
with sessions[request.path] as context:
try:
resp = context.eval(request.content.getvalue())
except PyV8.JSError, e:
return str(e).replace("JSError: ", '')
return str(resp)
def render_GET(self, request):
if request.path == '/':
request.redirect('/%s' % abs(hash(time.time())))
return "Redirecting to new context..."
else:
if not request.path in sessions:
sessions[request.path] = PyV8.JSContext(GlobalContext())
return """
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function handleKey(event) {
if (event.keyCode == 13) {
$('#output').append('\<div>&gt;&gt;&gt; ' + $('#input').val() + '\<\/div>');
$.post("%s", $('#input').val(), gotResponse, "text");
$('#input').val('');
}
}
function gotResponse(data, status) {
$('#output').append('\<div>' + data + '\<\/div>');
}
$(document).ready(function() {$('#input').val(''); $('#input').focus()});
</script>
</head>
<body>
<div id="output">
</div>
&gt;&gt;&gt; <input id="input" onkeydown="handleKey(event)" value="" />
</body>
</html>
""" % request.path
class GlobalContext(PyV8.JSClass):
def dir(self, obj):
return PyV8.convert(obj)
port = 8123
application = service.Application("scriptlets")
service = internet.TCPServer(port, server.Site(ContextResource()))
service.setServiceParent(application)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment