Path: blob/master/tests/multimechanize/test_scripts/client.py
447 views
import urllib21import websocket2import json3import uuid45root = "http://localhost:8888"67class SageCellSession(object):8def __init__(self):9f = urllib2.urlopen("%s/kernel" % (root,), "")10data = json.loads(f.read())11f.close()12self.kernel_id = data["kernel_id"]13self.ws_url = data["ws_url"]14self.iopub = websocket.create_connection("%skernel/%s/iopub" % (self.ws_url, self.kernel_id))15self.shell = websocket.create_connection("%skernel/%s/shell" % (self.ws_url, self.kernel_id))16self.session_id = str(uuid.uuid4())1718def __del__(self):19self.close()2021def __enter__(self):22return self2324def __exit__(self, etype, value, traceback):25self.close()2627def execute(self, code):28content = {"code": code,29"silent": False,30"user_variables": [],31"user_expressions": {"_sagecell_files": "sys._sage_.new_files()"},32"allow_stdin": False}33self.send_msg("execute_request", content)3435def update_interact(self, interact_id, values):36self.execute("sys._sage_.update_interact(%r, %r)" % (interact_id, values))3738def send_msg(self, msg_type, content):39msg = {"header": {"msg_id": str(uuid.uuid4()),40"username": "username",41"session": self.session_id,42"msg_type": msg_type43},44"metadata": {},45"content": content,46"parent_header":{}47}48self.shell.send(json.dumps(msg))4950def close(self):51self.iopub.close()52self.shell.close()5354def iopub_recv(self):55return json.loads(self.iopub.recv())5657def shell_recv(self):58return json.loads(self.shell.recv())5960def load_root():61resources = ["/", "/static/root.css", "/static/jquery.min.js",62"/static/embedded_sagecell.js",63"/static/jquery-ui/css/sagecell/jquery-ui-1.8.21.custom.css",64"/static/colorpicker/css/colorpicker.css",65"/static/all.min.css", "/static/mathjax/MathJax.js",66"/static/sagelogo.png", "/static/spinner.gif",67"/sagecell.html", "/static/all.min.js",68"/static/mathjax/config/TeX-AMS-MML_HTMLorMML.js",69"/static/mathjax/images/MenuArrow-15.png",70"/static/jquery-ui/css/sagecell/images/ui-bg_highlight-hard_60_99bbff_1x100.png",71"/static/mathjax/extensions/jsMath2jax.js",72"/static/jquery-ui/css/sagecell/images/ui-bg_highlight-hard_90_99bbff_1x100.png"]73for r in resources:74f = urllib2.urlopen(root + r)75assert f.code == 200, "Bad response: HTTP %d" % (f.code,)767778