Webexec example¶
The server executes an application (currently ping localhost) and sends the standard output to all connected browsers.
Server - examples/webexec.py¶
#!/usr/bin/env python
import example_pythonpath
from example_utils import ExampleServer, ExampleStaticFile
import gevent
from gevent import subprocess
class WebexecServer(ExampleServer):
urls = [
('/', ExampleStaticFile('webexec.html')),
] + ExampleServer.urls
def server_init(self):
gevent.spawn(self.exec_subprocess)
def exec_subprocess(self):
proc = subprocess.Popen(['ping', 'localhost'], stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline().rstrip()
if line is None:
break
self.sessions.notify('stdout', line)
if __name__ == '__main__':
WebexecServer.main()
Client - examples/webexec.html¶
<!DOCTYPE html>
<html>
<head>
<title>Mushroom Test Client</title>
</head>
<body>
<p id="output"></p>
<script type="text/javascript" src="/js/mushroom.js"></script>
<script type="text/javascript">
var client = new mushroom.Client({
url: '/'
});
client.connect();
client.method('stdout', function(request) {
var outputElement = document.getElementById('output');
var lineElement = document.createElement('div');
lineElement.innerText = request.data;
outputElement.appendChild(lineElement);
});
</script>
</body>
</html>