by Girish Venkatachalam
Girish Venkatachalam is a UNIX hacker with more than a decade of
networking and crypto programming experience.
His hobbies include yoga,cycling, cooking and he runs his own
business. Details here:
UNIX geeks belong to the command line world. They want to do everything
from google searches to flickr photo uploads from the almighty UNIX
command line. Since most websites nowadays have a REST API or similar
this is very much possible and even desirable in several cases.
Google has released pygoogle for google searches from the command line
and many options exist for accessing flickr photographs. Nearly every
online service including rememberthemilk todo lists, delicious bookmarks
and of course mail services like gmail include a way to access and
modify your online storage from the command line.
In this article we are going to look at a tiny project to access the
English dictionary from the command line. There is a C client invoked
using the dict(1) command on UNIX boxes. The dictionary protocol is a very
simple text based protocol described in RFC2229.
I wrote this Python script just for fun. I feel that both the dictionary
protocol and Python are useful tools to learn protocols and programming
respectively. Especially if you are a beginner.
sys.exit("Usage: " + sys.argv + " [dictionary server]")
pat = re.compile("250 ok")
pat2 = re.compile("\.|2\d\d");
pat3 = re.compile("1\d\d")
errpat = re.compile("552 no match");
errpat2 = re.compile("551");
#create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#now connect to the dictionary server on port 2628
# By default it connects to www.dict.org unless overridden by the
# command line
if len(sys.argv) == 3:
s.send("define * " + '"' + sys.argv.strip() + '"' + "\r\n\r\n")
buf = ''
tmpbuf = s.recv(1024)
if not tmpbuf:
buf += tmpbuf
sys.stdout.write("No definitions found for " + '"' +
sys.argv.strip() + '"')
s.send("match * lev " + '"' + sys.argv.strip() + '"' +
res = s.recv(80)
if errpat.search(res) or errpat2.search(res):
sys.stdout.write(", perhaps you mean:")
suggestions = s.recv(2048)
source = ''
for line in suggestions.splitlines():
if source != '':
prev = source
prev = ''
if pat.match(line) or pat2.match(line):
source = line.split(' ')
if prev == source:
sys.stdout.write(' ' + source.strip('"'))
sys.stdout.write('\n' + source + ':' + ' ' +
for line in buf.splitlines():
if line.find('" ') != -1:
a = line.split('" ')
b = str(a).split(' ');
c = ' '.join(b[1:])
d = c.split('"')
print "From " + d + ' ' + '[' + b + ']' + ":\n"
a = line.split(' ')
a = "found"
print ' '.join(a[1:])
sys.stdout.write(' ' + line + "\n")
Copy paste the above python code as dict.py and give execute permissions.
$ chmod +x dict.py
Now all you have to do to lookup a word is type this on the UNIX command
$ dict.py programming
This will lookup a dictionary server from the Internet and print the
meanings along with thesaurus lookups to the console. You might wish to
use a pager program like less(1) or redirect the output to a file.
Learning is easy when things are kept simple. Once we master the basics
we can over time graduate to master advanced concepts. Happy
The dictionary protocol
Got something to add? Send me email.
Increase ad revenue 50-250% with Ezoic
More Articles by Girish Venkatachalam
© 2013-06-21 Girish Venkatachalam