Using WSE3 services with Python

Like many companies, we use a number of Web Services at work. These are mainly used for our client side applications to talk back to things like SQL and other services, without going direct to them (and thus having a "security layer" of sorts). Having the ability to use these in some scripts/processes that you design can often be useful, as you have to play by the business logic.

In our case, our web services are .NET Web services using WSE3, to add on WS-Addressing and WS-Security features to them. These tie in easily to C# or other .NET languages, just by adding them into your Application. However, in a SysAdmin/DevOps role, you most likely aren't wanting to use a language like C#. You most likely want something like Python, Ruby or node.js; or you may be wanting to integrate these into something that you want to run on Linux or Solaris.

That was my thought. I wanted something in Python, to be able to run it from a few of our Linux boxes. On having a look around the internet, it seems that there is actually not much documentation on getting WSE3 (with WS-Addressing and WS-Security) web services to work in Python. So this article changes that.

In this article I will provide an example script which will show you how Python can talk to WSE3 scripts. It should hopefully point you in the right direction, that you need to go to achieve your goal.

What you need

Before we start, you will need to have available the following python libraries:

It will also help to have a small bit of knowledge about how SOAP works, but this is not required.

Our final script

We will start out, by showing the full script and then explaining each bit of how it works.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python

import logging
import random
import string

from suds import MethodNotFound
from suds.client import Client
from suds.wsse import Security, UsernameToken
from suds.sax.element import Element
from suds.sax.attribute import Attribute
from suds.xsd.sxbasic import Import

WEBSERVICE_URL = 'http://www.example.com/Webservice/Webservice.asmx'
NS_WSA = ('wsa', 'http://schemas.xmlsoap.org/ws/2004/08/addressing')
MUST_UNDERSTAND = Attribute('SOAP-ENV:mustUnderstand', 'true')

def main():
    logging.basicConfig(level=logging.INFO)
    logging.getLogger('suds.client').setLevel(logging.DEBUG)

    client = Client('%s?wsdl' % WEBSERVICE_URL)

    add_security(client, 'DOMAIN\User', 'Password')
    add_addressing(client, WEBSERVICE_URL)
    method = get_method(client, 'method')

    print method()

def add_security(client, user, passwd):
    sec = Security()
    token = UsernameToken(user, passwd)
    token.setnonce()
    token.setcreated()
    sec.tokens.append(token)
    client.set_options(wsse=sec)

def add_addressing(client, webservice_url):
    headers = []

    addr = Element('Address', ns=NS_WSA).setText('http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous')

    headers.append(Element('Element').addPrefix(p='SOAP-ENC', u='http://www.w3.org/2003/05/soap-encoding'))
    headers.append(Element('ReplyTo', ns=NS_WSA).insert(addr).append(MUST_UNDERSTAND))
    headers.append(Element('To', ns=NS_WSA).setText(webservice_url).append(MUST_UNDERSTAND))
    headers.append(addr)
    headers.append(Element('MessageID', ns=NS_WSA).setText('urn:uuid:%s' % generate_messageid()))

    client.set_options(soapheaders=headers)

def get_method(client, method):
    try:
        m = getattr(client.service, method)
        action = client.wsdl.services[0].ports[0].methods[method].soap.action
        action = action.replace('"', '')
    except MethodNotFound:
        return None

    action_header = Element('Action', ns=NS_WSA).setText(action)
    client.options.soapheaders.append(action_header)

    return m

def generate_messageid():
    fmt = 'xxxxxxxx-xxxxx'
    resp = ''

    for c in fmt:
        if c == '-':
            resp += c
        else:
            resp += string.hexdigits[random.randrange(16)]

    return resp

if __name__ == '__main__':
    main()

How the script works in detail

Now that we have seen the full script, we will go thru it, and explain what it does.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import logging
import random
import string

from suds import MethodNotFound
from suds.client import Client
from suds.wsse import Security, UsernameToken
from suds.sax.element import Element
from suds.sax.attribute import Attribute
from suds.xsd.sxbasic import Import

First we start by including some libraries we are going to use. We use the random library for generating a random message ID for our SOAP message. The includes from suds.* are for the SOAP library we will be using.

We have also included the logging library, which will enable us to be able to see some debug messages should we wish to.

1
2
3
WEBSERVICE_URL = 'http://www.example.com/Webservice/Webservice.asmx'
NS_WSA = ('wsa', 'http://schemas.xmlsoap.org/ws/2004/08/addressing')
MUST_UNDERSTAND = Attribute('SOAP-ENV:mustUnderstand', 'true')

Next, we define our webservice url (WEBSERVICE_URL). This should be the full path to the endpoint. This should not be the URL to it's WSDL.

We also define some attributes for our XML Elements that we will needed further down in our script. NS_WSA is the namespace we are defining for WS-Addressing. The MUST_UNDERSTAND variable is used to make Elements which the Web Service must understand and process.

1
2
3
def main():
    logging.basicConfig(level=logging.INFO)
    logging.getLogger('suds.client').setLevel(logging.DEBUG)

This is our main() function, that will be called when we start the script. The first two lines, will provide our debug output for running the script. These can be commented out afterwards should you want to.

1
client = Client('%s?wsdl' % WEBSERVICE_URL)

We next create an instance of the suds.client.Client object, which will be used for connecting and inspecting our web service.

1
2
3
4
5
add_security(client, 'DOMAIN\User', 'Password')
add_addressing(client, WEBSERVICE_URL)
method = get_method(client, 'method')

print method()

The last bit of our main() function, will be where we call our other functions to add in support for WS-Addressing and WS-Security. Then the last bit will be finalising the method we are going to call, and calling the method and printing out the results.

You would change/set the DOMAINUser and Password, as well as the method here, so it runs with the right credentials and the correct method of the web service.

1
2
3
4
5
6
7
def add_security(client, user, passwd):
    sec = Security()
    token = UsernameToken(user, passwd)
    token.setnonce()
    token.setcreated()
    sec.tokens.append(token)
    client.set_options(wsse=sec)

This is our add_security() function which will take care of implementing WS-Security into our request. Luckily for us, WS-Security support is available in Suds already, so we just need to add it to our client object.

The token we add is our username and password (username is prefixed with the Domain when it is sent to the function). We also set a "Nonce" and the created date and time. These together help to prevent replay attacks with the SOAP request.

Finally we add our token to our security object, and add our security object, into our client object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def add_addressing(client, webservice_url):
    headers = []

    addr = Element('Address', ns=NS_WSA).setText('http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous')

    headers.append(Element('Element').addPrefix(p='SOAP-ENC', u='http://www.w3.org/2003/05/soap-encoding'))
    headers.append(Element('ReplyTo', ns=NS_WSA).insert(addr).append(MUST_UNDERSTAND))
    headers.append(Element('To', ns=NS_WSA).setText(webservice_url).append(MUST_UNDERSTAND))
    headers.append(addr)
    headers.append(Element('MessageID', ns=NS_WSA).setText('urn:uuid:%s' % generate_messageid()))

    client.set_options(soapheaders=headers)

The next function we call in our script, is the add_addressing() function. This function adds most required headers for WS-Addressing. There is one that is added the get_method() function, but we will cover that shortly.

First we start by defining an "Address" Element, which is a static reference to the WS-Addressing specification. Note we also set the namespace to the NS_WSA variable we created befoer. After that, we create a generic Element tag, which is used to define the SOAP Encoding we are using.

Next are the two most important elements we will add to the header. The ReplyTo Element is added, with reference to the Address element, and the mustUnderstand attribute. The ReplyTo element defines the endpoint for the reply to the web service. The To Element is where we define the address of the web service we are calling.

We finish up by adding the Address element into the headers, and defining a unique MessageID for the message we are able to send. We then add these headers into our client object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def get_method(client, method):
    try:
        m = getattr(client.service, method)
        action = client.wsdl.services[0].ports[0].methods[method].soap.action
        action = action.replace('"', '')
    except MethodNotFound:
        return None

    action_header = Element('Action', ns=NS_WSA).setText(action)
    client.options.soapheaders.append(action_header)

    return m

This function, get_method(), is used to be able to find the action path of the method we want to call, and add this int our SOAP headers.

We first look up to see whether the method we have been given, is a valid method that exists when the WSDL file is introspected. If it is, we store this in m, for returning back later.

Before finishing up however, we look in the WSDL definition for the service, and get the SOAP action attribute for the given method. We remove and bad characters and add this into our SOAP Headers as the Action element. This tells WS-Addressing the function we want to call to do our work.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def generate_messageid():
    fmt = 'xxxxxxxx-xxxxx'
    resp = ''

    for c in fmt:
        if c == '-':
            resp += c
        else:
            resp += string.hexdigits[random.randrange(16)]

    return resp

The generate_messageid() function is used by add_addressing(), to generate a Unique and random message ID. This function simple look over each character in our defined format (fmt), and generates a unique hex character to be returned. Literal dashes (-) in the format string are ignored.

1
2
if __name__ == '__main__':
    main()

To finish off, this is stock standard python. If this script is invoked from the command line, the built-in variable name will be set to main. We simply say here, that if it is invoked from the command line, run our main() function.

Now, if you have updated some of the variables where needed in the above, you will now be able to run the script, and it should print our the resulting dataset from your web service. If you get any errors, have a look and see if you can work out what may be causing it. Do check that it's something as simple as maybe the wrong username or password tho.

Where to from here?

Now that you have the script, you will most likely want to use what you have learnt here, to form this into a library of functions, so you can use it in your application. And get the values from somewhere else, than what is hardcoded in the script.

You will also want to have a play around with calling method's with parameters and also seeing what happens when you start to need to use more complex data types that the web service method defines. But you should be able to handle this, as Suds includes this introspection in it's API support.

Should you have any questions, feel free to contact me, and i'll see what I can do!

I should also thank the below articles/posts on websites for helping, as without them, I may have had no clue to some bits of this.

Published: 12 March 2012 # — Tags: python, work

Fall 2011 TV Scorecard

Just thought I would give an update on the shows I said I would be watching mid-last year, and how they are going so far.

Let me know how you think these shows did, and if you think differently to how things are going below.

Boss:   I predicted this one wouldn't last, and I've been proven wrong. Another 10 episodes for Season 2 of the show have already been ordered. This is a bit weird, as each episode only managed to pull in about half a million viewers on average.

Charlie's Angels:   As I said in the original article, remakes don't always go well. Charlie's Angels managed to prove that, and was cancelled after only 4 episodes.

Grimm:   Of the two Fairy Tale based TV Shows to air during this Fall TV season, Grimm seems to be doing okay. However, some weeks, the other fairy tale based show, Once Upon a Time, is pulling in almost double the number of viewers. However, this is one of ABC network's best new shows for this season.

Homeland:   As to be expected, Showtime once again pulled a terrific storyline out of this show, and it ended up being one of the best rating Showtime shows. The story lends itself to a Season 2, but will Showtime?

How to Be a Gentleman:   "Probably won't last". And it didn't.

I Hate My Teenage Daughter:   It is rating well, but had a very long hiatus (December to March). I get the feeling this one will say around this year, but if it comes back, it could be questionable.

Last Man Standing:   Last Man Standing has rated very well, and I'm sure this will be something that we see for at least the next 2 seasons.

Man Up:   While the viewer numbers were okay, the target market was giving it a very low rating. This show was probably killed before it's time. But it's gone now, and it won't be coming back.

New Girl:   Massive Hit. Will be around for next season.

Once Upon a Time:   This is the show that has been beating Grimm in the ratings, and I think we are going to see it stay that way. Season 2 while unconfirmed, is a definite to happen.

Person of Interest:   This is personally, one of my favourite shows of the 2011-2012 Fall Season. In the US, ther ehas not been an epsideo which has pulled in less than 11 Million viewers, with the average in the 13 Million mark. This show isn't going to die, unless they change the storyline.

The Playboy Club:   This may have been the most hyped show of the Fall Season, but it was the first show to fall. Only 3 episodes were shown before the plug was pulled, and that was probably 3 too many.

Prime Suspect:   It's a UK Drama, that was converted for the US Market. and it failed. The show got off on a slow start, which made it hard for any audience to adapt to it, especially in the face of Shows that were on at the same slot. At least all the episodes got to be aired of this show.

Terra Nova:   Terra Nova finished it's 13 episode run a few months ago, but yet there is no clue on whether there will be a Season 2. While it did have fairly decent ratings, there is some thoughts that it maybe wasn't as good as what could have been. If we see this next season, it could be a miracle. Update Strike that, it's been canned.

Unforgettable:   Hey guess what, it has a love storyline! And it's with the two lead actors. So obvious that was going to be the case. But on the show itself, it seems to be doing okay on the ratings. Averaging about 10 million viewers, it should easily have a second season, but no word yet.

Published: 5 March 2012 # — Tags: shows, tv

Now available via IPv6

Quick Update - This site is now available via IPv6.

You don't have to do anything special, you will just use IPv6 if it is available and you have preferences for IPv6 connections.

If you want to test to see if you can get to the IPv6 site, try visiting http://v6.nullis.net

Published: 22 January 2012 # — Tags: ipv6

Steve, Thanks.

It was in High School that my passion for Technology, especially computing, was the most prominent. While I had dabbled with a few PC's and learned BASIC before then, it was High School I became intrigued even more, and was involved in setting up over 300 Macintosh PC's, from LC575's, PowerMac' to iMac's (Bondi Blue ones too!), and some G3's.

Having that experience definitely changed my Life Path, and opened a few more doors than would have been otherwise.

Update: Then again, the first PC I used at Primary School was an Apple IIe. It was the bomb.

And for that, I say: Steve, Thanks.

Steve Jobs, co-founder of Apple, passed today.

Published: 6 October 2011 # — Tags: apple, technology

Fall 2011 TV Shows - What I will be watching

Over the next month, is what the US TV Networks consider the start of their Fall Schedule, often this is when most of the dramas you have come to know and love start (NCIS, Friends, etc.).

So, I figured this would be a perfect time to look at, and discuss what shows I will be keeping an eye on during 2011-2012's Fall Schedule

The following are listed in alphabetic order, and are the shows that I will make an effort to watch at least the first few episodes of, and more if they turn out to actually be okay.

It is not uncommon that some of these series may even be pulled before that many episodes have aired. The network are getting better at picking what will succeed and what won't... but there is room for failure).

If you have any shows you think are worth watching, let me know via Twitter - @pwae.


Boss Logo

Boss

Overview: Mayor of Chicago, Kelsey Grammer, has been diagnosed with a neurological disorder and tries to plot his way to become the Governor of Illinois, while keeping his disorder a secret.

My thoughts: This is one I consider on the edge. If it stays on my to watch list for very long at all, will be a miracle. Giving it a chance.

More information: Boss on Wikipedia and Boss Trailer on Omelete (The video is available on Youtube, but only if you are in the U.S.).


Charlie's Angels Logo

Charlie's Angels

Overview: You know this drill, 3 girls, 1 guy and a voice on the end of a speaker box.

My thoughts: Remakes don't always go well, especially when the previous was such a hit (It worked for Hawaii Five-O didn't it?). Best of luck to these guys, they are going to need it.

More information: Charlie's Angels on Wikipedia and Charlie's Angels on Youtube.


Grimm Logo

Grimm

Overview: Cop Drama, where all the crimes relate back to a Grimm Brothers Story Tale (e.g. Little Red Riding Hood).

My thoughts: Has potential, but another TV Network has a story with almost the exact same story basis. Which one will be better?

More information: Grimm on Wikipedia and Grimm on Youtube.


Homeland Logo

Homeland

Overview: A lost at war spy, is found during a raid of another enemy site during another war conflict. He is bought home to be a hero, but some people think he has been turned, and is now a spy for the enemy.

My thoughts: Plot looks solid, and the trailer draws you in. Showtime have a history of having good shows around topics like this. (e.g. Sleeper Cell). Here's hoping it lives up to what's been hyped.

More information: Homeland on Wikipedia and Homeland on Youtube.


How to Be a Gentleman Logo

How to Be a Gentleman

Overview: Based on the non-fiction book by John Bridges, a comedic look at being a Gentlemen in the 21st Century with a story around it.

My thoughts: It's hard to find a real plot outline for this on the Internet. So let's just say, it's a comedy and hopefully funny. Probably won't last, unless it really hits the funny bone constantly.

More information: How to Be a Gentleman on Wikipedia and How to Be a Gentleman on Youtube.


I Hate My Teenage Daughter Logo

I Hate My Teenage Daughter

Overview: Two mothers who are trying to make sure their daughters aren't turning into the type of girls that tormented them at High School. But they slowly realise, they haven't done it all right.

My thoughts: Should be a good comedy, staring Jaime Pressly from My Name is Earl. The trailer sells it well, and I think it will last for a few seasons at least. Then again, maybe we are too overdone with family comedies?

More information: I Hate My Teenage Daughter on Wikipedia and I Hate My Teenage Daughter on Youtube.


Last Man Standing Logo

Last Man Standing

Overview: Tim Allen comes to realise the world is being overtaken by Women, and no longer is it a Man's World, it's a Women's World, especially with his wife and three daughters.

My thoughts: Tim the Toolman Taylor is back on TV! While he is going to be a big draw card for the Show, the show seems like a lot of the story line could be right from Home Improvement. Potential, at least 1 season, maybe 2.

More information: Last Man Standing on Wikipedia and Last Man Standing on Youtube.


Man Up Logo

Man Up

Overview: Three guys, who still want to be kids, decide it's time they grow up and act their age. But that's not as easy as you think.

My thoughts: I do love the opening to the trailer. If the rest of the show is like that, there is a chance of a season out of it. If it's not, gone before the first season is over.

More information: Man Up on Wikipedia and Man Up on Youtube.


New Girl Logo

New Girl

Overview: Zooey plays a well-liked, fun and cute woman in her late 20's. She unfortunately she had a sudden break-up with her boyfriend and is now looking for a new place to stay. She finds a place with 3 guys...

My thoughts: Like most of these shows, it has a lot of potential, and has already been nominated as one of the great new series for 2011. I hope it lasts, as I am dying to watch it after the trailer.

More information: New Girl on Wikipedia and New Girl on Youtube.


Once Upon a Time Logo

Once Upon a Time

Overview: Classic fairy tales come to life, but in the present day in the town of Storybrooke.

My thoughts: Remember how I said two networks had done TV Shows that sounded the same? Yep. this is the other one. Again, which one will actually be half-decent.

More information: Once Upon a Time on Wikipedia and Once Upon a Time on Youtube.


Penn & Teller: Tell a Lie

Overview: A 6-part series which will contain 6-7 stories per episode, one of which is a lie. Can you guess which one?

My thoughts: So Bullshit! is dead, and this is the new series (What happened Showtime?). I suppose there is just one question everyone has, will there be blatant nudity?

More information: Penn & Teller: Tell a Lie on Wikipedia and Penn & Teller: Tell a Lie on Youtube.


Person of Interest Logo

Person of Interest

Overview: A billionaire develops a computer program to predict people who are connected to a future violent crime. But he can't tell if they are the Perpetrator, the Victim or a Witness.

My thoughts: Not a very unique storyline, but definitely not a copy of another show directly. One of it's Execute Producers is J J Abrams, so it is worth a test, and looks quite interesting.

More information: Person of Interest on Wikipedia and Person of Interest on Youtube.


The Playboy Club Logo

The Playboy Club

Overview: Playboy is not a Club, it's a lifestyle. A story based on the first Playboy Club that opened in Chicago during the 60's, features the Man, the lifestyle and the Bunnies.

My thoughts: This is probably one of the most hyped series to be starting this Fall, and probably also the most controversial (maybe they never saw Californication?). It is worth watching and finding out if it's going to flop or not.

More information: The Playboy Club on Wikipedia and The Playboy Club on Youtube.


Prime Suspect Logo

Prime Suspect

Overview: Based on the British Series, it is the American version of a Crime Drama, based around a Female Detective, who is trying to make it in a male dominated Homicide department

My thoughts: UK Dramas that become US Dramas often fail. Exceptions exist, but failure is usually the answer. However the trailer for this one impressed me, and shows it could be damn good. Please don't stuff it up, and we will get a recurring series.

More information: Prime Suspect on Wikipedia and Prime Suspect on Youtube.


Terra Nova Logo

Terra Nova

Overview: Science Fiction drama which bases it on people from the year 2149, going back 85 million years in time, to start over, and save the human race from destruction.

My thoughts: One word, Dinosaurs. No one does a good series with Dinosaurs. Let's see if they can make it stick. Would be certainly different.

More information: Terra Nova on Wikipedia and Terra Nova on Youtube.


Unforgettable Logo

Unforgettable

Overview: A former NYC Detective who has a medical condition where she can remember everything. She is asked to help out Homicide, and ends up joining Homicide to help them with her special skills.

My thoughts: There is a possibility here. I hope this doesn't end up anything like what Medium did (you know, boring, repetitive, and did I mention boring?). Ten to one there is going to be an alternate love storyline in this one.

More information: Unforgettable on Wikipedia and Unforgettable on Youtube.


So, that's my idea on what will be good this season. Of these 16, there is a good chance we will only have a few of them left for a 2nd season. But then, that's how the cookie crumbles.

Published: 19 September 2011 # — Tags: shows, tv