RSS

Category Archives: 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: , , , ,

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

 
1 Comment

Posted by on May 5, 2008 in Uncategorized