October 2007 Archives
Tue Oct 30 01:16:36 EDT 2007
Yet even more Python tricks!
Yes folks, Python is truly a programmer's goldmine. Today we're going to do something Web 2.0-worthy with Python. We're going to create a server written in Python. But not just any old server. We're going to create an XML-RPC server, and we'll also write up a tiny client for it as well to demonstrate how surprisingly simple it is to build a server.
First, let's write up the server itself. Let's call this file "xmlrpc_server.py":
#!/usr/bin/env python
import SimpleXMLRPCServer
#set server address(local here)
servAddr=("localhost",8080)
#define server functions and instances here
def areaSquare(length):
return length*length
def areaRectangle(length,width):
return length*width
def areaCircle(radius):
return 3.14*(radius*radius)
class MyFuncs:
def say(self,line):
return "#"+line+"#"
#create the server object
server=SimpleXMLRPCServer.SimpleXMLRPCServer(servAddr)
funclist=(areaSquare,areaRectangle,areaCircle)
#register functions
for eachfunc in funclist:
server.register_function(eachfunc)
#register instance
server.register_instance(MyFuncs())
server.register_introspection_functions()
#start serving
server.serve_forever()
To start up this little server, type "python ./xmlrpc_server.py
&" on the command-line. Now, let's write a little demo client
to show that we can actually use the server. This file will be
called "xmlrpc_client.py":
#!/usr/bin/env python
import xmlrpclib
#connect to a server
servAddr="http://localhost:8080"
s=xmlrpclib.ServerProxy(servAddr)
#list available server methods
for m in s.system.listMethods():
print m
#call server methods
print "Areas:"
print "Square:",s.areaSquare(5)
print "Rectangle:",s.areaRectangle(2,6)
print "Circle:",s.areaCircle(5)
print s.say("E=m(c*c)")
Simply run "python ./xmlrpc_client.py", and you'll see that we are
actually using functions stored remotely on the server. Pretty
cool, right? No? Well, you haven't seen anything yet. You see, this
XML-RPC stuff can be used within web services, which shockingly
includes scripted objects within Second Life. With XML-RPC you can
create objects networked together to do things that would be
difficult, slow, or plain impossible to implement. I'll get more
into this in my next post.Tue Oct 9 23:29:36 EDT 2007
Second Life and Open Source
In case you didn't know, for almost 1 and 1/2 years I have been
addicted to one MMOG: Second
Life. What separates SL from other games such as There, World
of Warcaft and others is that whatever you create within SL, you
own it and therefore can sell it. This has engendered many in-world
businesses and business models to flourish, including open source.
Even I've gotten into the business act. 
I've opened up a store on the newly created sim Ama Gi(which is ancient Sumerian for "freedom"), an island sim dedicated to the ideals and promotion of FOSS and free culture. My store is called simply "Antonius Misfit's Open Source Shop", where I sell some skyboxes, but mainly scripted "laptop" computers(all open source, of course). My latest creation is something still in development, but I'm very proud of it as it is my first product from scratch. It's a GPLv3-licensed build packager and rezzer I call "Apt-Rez". Of course, Apt-Rez isn't the only product of it's kind available(such as Rez-Foo and Rez-Faux), but I feel it perfectly serves as not only a ready-to-use solution for builders, but also as a learning aid for budding LSL scripters or a starting point to roll your own rezzer. Plus, Apt-Rez is also available for purchase either at my shop or on SL Exchange for a nominal price(Yes, the GPL says it's perfectly acceptable to sell your own open source items as long as the source is freely attainable, something that apparently even some open source developers haven't grokked yet).
Wed Oct 3 10:27:34 EDT 2007
More Ubuntu Programming Gems
In my last entry, I introduced Python as a quick and easy way to create GUI programs for Ubuntu. However, Python has a super-handy module for creating your own command-line interfaces, known simply as cmd. Specifically, cmd contains a minimal class representing a command-line shell, called Cmd. To create your own shells, you must subclass cmd.Cmd and add your own functionality there. Here's a minimal example of how to create a command-line shell wth Python:
#!/usr/bin/env python
import os,sys,cmd,shlex,readline
class Shell(cmd.Cmd):
def __init__(self,intro="Welcome to Shell):
cmd.Cmd.__init__(self,intro)
def do_prompt(self,line):
"Set the command prompt"
self.promptbackup="(Cmd) "
if line=="":
self.prompt=self.promptbackup
else:
self.prompt=line
do_man=do_help
def do_exit(self,line):
"Exit Shell"
return True
if __name__=="__main__":
Shell().cmdloop()
As you can see, it's a fairly trivial affair. Actual commands are
implemented as methods that begin with "do_". Exiting the shell
requires a method that returns True. You can even create scripts
for your shell, just like with any standard *nix shell or Windows
batch file.Tue Oct 2 00:09:23 EDT 2007
Ubuntu Programming Secrets
Ever since Ubuntu Linux arrived on the scene three years ago, it has become the most popular distribution in the world, and perhaps the one to bring Linux to the completely non-geek masses. But even though Ubuntu seems to be a desktop-only distribution at first glance, there's a lot of programming power built-in even before the first time the "Add/Remove..." app or Synaptic is run. Here's the rundown of Ubuntu's three hidden programming gems:
GUI programming with Python
While many open source programs are written in C or C++, those
languages are too cryptic for average users to grasp. Ubuntu ships
with the interpreted and easy to learn Python language, along with
full GUI programming modules(Gtk/Gnome or Qt for Kubuntu). Hidden
away in Ubuntu's menu editor is an entry to run a Python shell
interactively. To activate the entry, right-click the Applications
menu and select "Edit Menus". In the "Menus:" section of the
editor, go to Applications->Programming. You'll see two items
show up in the "Items:" section. Check the box next to "Python
(v2.5)", and close the menu editor. Now you will find the entry
activated under Applications->Programming->Python, and an
interactive Python terminal will pop up for you to learn and
experiment.
Extending The File Manager
On Ubuntu, you can easily extend the functionality of the file
manager, Nautilus, through executable scripts. They can be written
in Python, Perl, and even bash. To use a Nautilus Script, place the
script in the $HOME/.gnome2/nautilus-scripts directory, select any
files/folders to be passed to the script, then right-click your
selection and navigate to Scripts->YourScript. To get a good
idea of what scripting Nautilus can do for you, I've created a
collection of scripts used for
programming, multimedia conversion, and even blogging(I'm using my
"BlogShell.sh" script right now to make this entry). Kubuntu users
have a similar feature called "service menus". You can find an
excellent source of service menu scripts here.
Creating GUI dialogs on the command-line
Going hand-in-hand with file manager extensions are the tools
zenity(for Gnome/XFCE) and kdialog(for KDE). These comman-line
tools can create several GUI-based dialogs that are used within
shell scripts. Ubuntu has excellent documentation for zenity,
obtainable by clicking the "?" icon next to the main desktop menu
and entering "zenity" in the search box. Kubuntu users need only
enter "man:kdialog" within Konqueror.