This recipe is the JavaScript client side of an overall recipe that shows how to do system monitoring using WebSockets, Python, Go (using websocketd), and JavaScript + HTML. The server side of the recipe (in Python, and using websocketd which is written in Go), is here:
The system monitoring example shows the system disk space info (total, used and free) using the Python psutil module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!DOCTYPE html>
<html>
<head>
<title>
Disk space monitoring with websocketd (Go) and psutil (Python).
</title>
</head>
<body>
<h3>
Disk space monitoring with websocketd (Go) and psutil (Python).
</h3>
<p>
<div id="log"></div>
</p>
<script>
// helper function: log message to screen
function log(msg) {
document.getElementById('log').innerText += msg + '\n';
}
// setup websocket with callbacks
var ws = new WebSocket('ws://localhost:8080/');
ws.onopen = function() {
log('CONNECT');
};
ws.onclose = function() {
log('DISCONNECT');
};
ws.onmessage = function(event) {
log('MESSAGE: ' + event.data);
};
</script>
</body>
</html>
|
Please refer to this recipe for the server side of this example:
Run the server side first (as shown at the above link), and then open the code shown in this recipe you are reading now, in a WebSocket-enabled browser.
This post has more details:
http://jugad2.blogspot.in/2014/01/websocketd-and-python-for-system.html
Hello, I've tried to implement this example https://account.activestate.com/signin/?_ga=1.90995225.2017638630.1466061820&next=https%3A%2F%2Fcode.activestate.com%2Frecipes%2F578806-javascript-websocket-client-for-python-go-websocke%2F, but I can't install websocketd, or I didn't understand it. When I run in my cmd this command websocketd --port=8080 python psutil_disk_usage.py, I have this issue: "websocketd" is not recognized as an internal or external command. I've tried to download an archive websocketd-0.2.12-windows_amd64.zip from here https://github.com/joewalnes/websocketd/releases, but I don't know if it's well. I have Windows64, I put the .exe file in the same folder where are psutil_disk_usage.py and psutil_disk_usage.html. I also run the .exe file, but no chance.
Thank you for you help,