Welcome, guest | Sign In | My Account | Store | Cart

This is the server side of a recipe that shows how the use websocket, a tool written in Go, that sort of daemonizes a program that reads from standard input and writes to standard output, thus making it into a WebSocket server.

Python, 24 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
An example program that writes to STDOUT:

# 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)

And a websocketd command that makes the above program into a WebSocket server running on port 8080:

websocketd --port=8080 python psutil_disk_usage.py

You have to give the command:

set PYTHONUNBUFFRED=true

before the above websocketd command is given.

A more detailed description is given here in [this blog post](http://jugad2.blogspot.in/2014/01/use-websockets-and-python-for-web-based.html)

1 comment

Vasudev Ram (author) 10 years, 3 months ago  # | flag

Some issue with the markdown syntax in the last line of the above description, sorry - it didn't render the link to the blog post correctly.