I got to know about websocketd recently, via a reply to this question I posted on Hacker News: Ask HN: What are you using Go for?
websocketd is "a small command-line tool that will wrap an existing command-line interface program, and allow it to be accessed via a WebSocket". It's written in Go, by Joe Walnes.
He describes websocketd as "Like inetd, but for WebSockets. Turn any application that uses STDIN/STDOUT into a WebSocket server.".
The websocketd README goes on to say:
[ WebSocket-capable applications can now be built very easily. As long as you can write an executable program that reads STDIN and writes to STDOUT, you can build a WebSocket server. Do it in Python, Ruby, Perl, Bash, .NET, C, Go, PHP, Java, Clojure, Scala, Groovy, Expect, Awk, VBScript, Haskell, Lua, R, whatever! No networking libraries necessary. ]
Websocket topic on Wikipedia
So I wrote a small Python program to try out websocketd. It uses the psutil module to get disk space info (total, used, and free) from the system.
(I had blogged about psutil earlier, here:
psutil, Python tool to get process info and more.)
Here is the code:
# psutil_disk_usage.py import string from time import sleep import psutil print "Disk Space (MB)".rjust(46) print " ".rjust(25) + "Total".rjust(10) + "Used".rjust(10) + "Free".rjust(10) for i in range(5): du = psutil.disk_usage('/') print str(i + 1).rjust(25) + str(du.total/1024/1024).rjust(10) + str(du.used/1024/1024).rjust(10) + str(du.free/1024/1024).rjust(10) sleep(2)
When this program is run directly at the prompt, with the command:
, it gives this output:
Disk Space (MB) Total Used Free 1 99899 91309 8590 2 99899 91309 8590 3 99899 91309 8590 4 99899 91309 8590 5 99899 91309 8590Running this program under the control of websocketd, with the command:
websocketd --port=8080 python psutil_disk_usage.py
, causes the output of the program to go to the browser that is listening on port 8080 (see below *).
You have to:
at the command line first, for it to work as a WebSocket server; it works fine as a plain command-line program, without that setting.
See this StackOverflow question.
- Vasudev Ram - Dancing Bison Enterprises
Vitamins & Supplements