Showing posts with label Go-language. Show all posts
Showing posts with label Go-language. 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, June 1, 2013

Video: Fireside chat, Go team at Google I/O 2013

Watch "Google I/O 2013 - Fireside Chat with the Go Team" on YouTube

Watched some of it. Audio not very good (on mobile) but can still hear a good amount. Probably better on desktop.

Wednesday, January 2, 2013

Using Go to create web apps - post and threads

Thoughts on Go after writing 3 websites : programming

First saw it on Hacker News.

The above thread is on Reddit.

Interesting stuff.

Thursday, December 13, 2012

The Go Playground lets you enter and run Go code online

Go Playground

It even works some from my Android mobile's browser.

Can be useful to write and run snippets of code on  the go :-)

Also to run code from your desktop / laptop, whether or not you have Go installed locally. All you need is a reasonably modern browser and an Internet connection.

The Go Tour - tour.golang.org - also lets you do this, with the added benefit that you can modify their examples and then run them.

- Vasudev Ram
www.dancingbison.com

Thursday, October 18, 2012

goupx can shrink Go language binaries

By Vasudev Ram


I have been using the Go language lately, and noticed that the binaries it creates when you compile your source code can be large, since they statically link everything needed to run your Go program. (A simple Hello World program binary can be on the order of magnitude of 1 MB in size.)

(Some of the posts I checked say that Go supports dynamic linking via gccgo, a front-end to the gcc compiler. Not sure right now what the difference is between dynamic linking and dynamic loading (of libraries, as is done with C. Need to check.)

goupx is a possible solution to this issue. It seems to be based on UPX (Ultimate Packer for eXecutables), a utility for compressing executable files.

I had come across UPX some years ago, and tried it out a bit then. Today I again checked the UPX web site and was interested to see that it supports compressing executables for several platforms, including Linux, Mac OS X and others, not just Windows.

From the goupx site:

[ Resulting filesizes are typically 25% of the original go executable. Your mileage my vary. ]

goupx seen via this comment on proggit (part of a thread about learning Go).

- Vasudev Ram - Dancing Bison Enterprises


Friday, October 12, 2012

otto, JavaScript parser and interpreter written in Go.


By Vasudev Ram

Seen via @go_nuts (Twitter account for the go_nuts Go language mailing list):

Package otto is a JavaScript parser and interpreter written natively in Go.

- Vasudev Ram - Dancing Bison Enterprises



The Go language FAQ is interesting

By Vasudev Ram


Start reading it here: Origins of Go.

- Vasudev Ram - Dancing Bison Enterprises


Go by example: Online set of Go language examples

By Vasudev Ram


Go by example is a recently started site. It has examples for various topics in the Go programming language (often called golang), which was created by Google a few years ago, and is currently getting a good amount of interest among programmers.

Article by Mark McGranaghan introducing Go by example.

Twitter account for Go by example.

Hacker News post announcing Go by example.

For background: Go language on Wikipedia.

- Vasudev Ram - Dancing Bison Enterprises

Friday, September 14, 2012

Google's Go language getting popular?

Google's Go language:

http://golang.org

I've been seeing various posts about the Go language recently.

See:

Go language on Wikipedia

http://go-lang.cat-v.org/organizations-using-go

Take with a pinch of salt due to possible hype, but still:

http://gigaom.com/cloud/will-go-be-the-new-go-to-programming-language/

HN thread about above GigaOm post:

http://news.ycombinator.com/item?id=4516131

Older but interesting:

Ask HN: Who is using Go language?

Inspired by nature.
- dancingbison.com | @vasudevram | jugad2.blogspot.com