Showing posts with label client-server. Show all posts
Showing posts with label client-server. Show all posts

Friday, January 3, 2014

Use WebSockets and Python for web-based system monitoring

By Vasudev Ram

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:

python psutil_disk_usage.py

, 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      8590
Running 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:

set PYTHONUNBUFFERED=true

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.

(*) You also have to write a WebSocket client, i.e. an HTML page with JavaScript, that the server can connect to, and send data to. The JavaScript code listens for a connection and then reads the data sent by the server and displays it on the web page.

In my next post, I'll show the JavaScript WebSocket client, which is a modified version of an example on the websocketd page.

- Vasudev Ram - Dancing Bison Enterprises

Contact Page

Vitamins & Supplements







Saturday, May 28, 2011

Resolver's PythonAnywhere, browser-server development tool

By Vasudev Ram

dancingbison.com | @vasudevram | jugad2.blogspot.com



PythonAnywhere from Resolver Systems is a new type of client-server Python development tool. It is not yet available, but you can sign up for the free private beta starting in a few weeks, at the preceding link for PythonAnywhere in this paragraph. Resolver Systems is also the company behind the Resolver One spreadsheet, which allows you to enter expressions in Python, and related products - see their products page for more information.

Some of the features and benefits cited for it (excerpted from their page) are:

- "a combined Python console and development environment that displays in your web browser and runs on our servers"

- "easy to create and run Python programs in the cloud"

- "Start work on your desktop machine, then later pick up from where you left off by accessing exactly the same session from your tablet"

- "When you print things out, instead of getting simple text, you get something you can interact with. Dictionaries and objects become expandable trees, ..."

- "If you're called away from your computer, you can log in from anywhere else and pick up exactly where you left off, with exactly the same command line and all of your variables and data just the way they were" --- I like this feature.

- "PythonAnywhere runs on super-powerful servers hosted by Amazon EC2, and you can take full advantage for that. Without paying a penny, you can run simple Python programs to help you learn. For heavy-duty processing, you only pay for what you use"

- "PythonAnywhere is preconfigured with loads of useful libraries, like NumPy, SciPy, Mechanize, BeautifulSoup, and many others"

- "Basic access to our servers, if you just want to try out Python, go through the tutorials, or do a few calculations, is completely free, and you don't even have to sign in"

Other things cost, on a pay-per-use basis.

Seems worth checking out when available.

- Vasudev Ram - Dancing Bison Enterprises