Welcome, guest | Sign In | My Account | Store | Cart

This hack allows you to add a cookie/header to a SOAPpy request. It uses a keyword args all-through to pass your own transports down to the SOAPpy core. It uses the ClientCookie module to store the cookies generated and/or to send the cookies.

Python, 56 lines
 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
#
# Author : t3rmin4t0r
# Mail   : gopalv82 -AT- yahoo.com
# Site   : http://t3.dotgnu.info/
# 

import sys, os, string
from SOAPpy import WSDL,HTTPTransport,Config,SOAPAddress
import ClientCookie
import urllib2

Config.cookieJar = ClientCookie.MozillaCookieJar()
# Uncomment the following line if you have cookies.txt
# Config.cookieJar.load("cookies.txt")

class CookieTransport(HTTPTransport):
  def call(self, addr, data, namespace, soapaction = None, encoding = None,
    http_proxy = None, config = Config):

    if not isinstance(addr, SOAPAddress):
      addr = SOAPAddress(addr, config)
    
    cookie_cutter = ClientCookie.HTTPCookieProcessor(config.cookieJar)
    hh = ClientCookie.HTTPHandler()
    hh.set_http_debuglevel(1)

    # TODO proxy support
    opener = ClientCookie.build_opener(cookie_cutter, hh)

    t = 'text/xml';
    if encoding != None:
      t += '; charset="%s"' % encoding
    opener.addheaders = [("Content-Type", t),
              ("Cookie", "Username=foobar"), # ClientCookie should handle
              ("SOAPAction" , "%s" % (soapaction))]
              
    response = opener.open(addr.proto + "://" + addr.host + addr.path, data)
    data = response.read()

    # get the new namespace
    if namespace is None:
      new_ns = None
    else:
      new_ns = self.getNS(namespace, data)

    print '\n' * 4 , '-'*50
    # return response payload
    return data, new_ns

# From xmethods.net

wsdlURL = "http://www.doughughes.net/WebServices/fortune/fortune.cfc?wsdl"

proxy = WSDL.Proxy(wsdlURL, transport = CookieTransport)

print proxy.getFortune()

Generally most SOAP systems do not use cookies at all. But for legacy reasons, in this particular case the application protocol and auth systems were split, with an apache module checking for cookies authorizing users. The SOAP server built using php does not even understand the cookies and lacks the libraries required to parse them.

To write a SOAPpy client for such a scenario - I had to use the hack described above. This could be used in a lot of libraries to make them a lot more flexible than the authors imagine. After this hack, the system is flexible enough to add any random headers and is not limited to cookies (see SOAPAction header).

http://t3.dotgnu.info/blog/hacks/kw-2005-10-12-03-59.html describes how exactly it works with a simpler sample.