RSS

Monthly Archives: July 2011

Django-chat using xmpp

XMPP IM Client

So i started building an IM client app. It’s based on Django, python, Strophe, jquery, etc… Does the world need another Instant Messaging app? Who knows? My motivation is purely for the sake of learning the innards of XMPP. Very selfish.

So far it has been good. I’m using a really well written¬†book¬†authored by MetaJack and implementing all the tutorials. Learning by doing i guess.

Progress?

After my first day i already had a basic single user chat going. Now its nothing special but WOW is it fun. I only chat to my wife btw. There is something addictive about building an app and taking little steps forward. The more i code the more fun I have.

End Goal?

G-Talk is not what I’m after. A usable web app is the destination and it seems reachable everyday. If i get to learn some awesome new tech and get some friends to use it then good for me. Also this is sort of my baptism into “Dream, Build and Release”.

Dream, Build and Release?

So the whole idea is too stop procrastinating and actually do something. People (myself included) are always talking about what they want to do but never act on it. Django-chat is my move towards action.

Anyway… goto to github and take a look at the code if you are interested.

You will here more about Django-chat over the next few weeks.

DEADLINE FOR LAUNCH: 31 August 2011

 
Leave a comment

Posted by on July 29, 2011 in Uncategorized

 

urllib2, debug mode

So one of my work projects is sending some xml via http.

We use urllib, urllib2 to do all this work. (obviously)

I needed to check what was happening to the data being sent. Since most of the work is hidden by urllib my debugging was pretty much useless.

However urllib2 allows us to switch it to debug mode like such:

import urllib, urllib2
url = "some url"
xml = "some xml"

httphandler=urllib2.HTTPHandler(debuglevel=1)
opener = urllib2.build_opener(httphandler)
opener.addheaders = [('Accept', 'application/xml'), ('Content-Type', 'application/xml'),]
urllib2.install_opener(opener)

req = urllib2.Request(url)
req.add_data(xml)
resp = urllib2.urlopen(req)

Sorted!!!!

 
Leave a comment

Posted by on July 18, 2011 in Uncategorized

 

Tags: , , , , ,

Python, set’s awesomeness

So i have just learnt about sets. And I’m really impressed.
Example:

What is the best way to compare two lists?
Lets find all the unique items from list_a that are not in list_b.

First try

list_a = [1,2,3,4,5]
list_b = [4,5,6,7,8,9,0]

unique_list = []
for item_a in list_a:
if item_a not in list_b:
unique_list.append(item_a)

unique_list will contain [1,2,3]

2nd try

list_a = [1,2,3,4,5]
list_b = [4,5,6,7,8,9,0]
unique_list = [item_a for item_a in list_a if item_a not in list_b]

3rd try

set_a = set([1,2,3,4,5])
set_b = set([4,5,6,7,8,9,0])
unique_set = set_a.difference(set_b)

I like the 3rd try better. Nice, clean and neat.

This is just the tip, much more is possible with the set functionality.

More examples to come…

 
Leave a comment

Posted by on July 14, 2011 in Uncategorized

 

Tags: , , ,

Python , Send xml data as post request

So I needed to send xml data to a service. initially I did it like this:


import urllib, urllib2
url = "some url"
param_data = {'businesses': xml}
params = urllib.urlencode(param_data)
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
opener.addheaders = [('Accept', 'application/xml'), ('Content-Type', 'application/xml'),]
urllib2.install_opener(opener)
resp = urllib2.urlopen(url, params)

Essentially sending the xml as a param.

This works fine.
The consuming service however does not expect a post param. Instead it expects the xml as data in the body of the post. To be honest this is the first time I had come across this. Weird.

So after a bit of complaining, nagging, googling, interrogating urllib and asking a much more knowledgeable coder. I changed the last bit to this:


req = urllib2.Request(url)
req.add_data(xml)
resp = urllib2.urlopen(req)

I used the xml as is without urlencoding it.

So this change will add the xml to the post body.

To add a set of params:

param_data = {'stuff':"things", "more_stuff": "more_things"}
params = urllib.urlencode(param_data)

req = urllib2.Request(url)
req.add_data(params)
resp = urllib2.urlopen(req)

Nice and simple. The complicated things usually are.

For more reading up on urllib:

Official python docs

Doug Hellman stuff

Hope this helped?

 
Leave a comment

Posted by on July 1, 2011 in Uncategorized

 

Tags: , , , ,