ActiveState Code

Recipe 473883: web sevice usage via a http proxy


This is a simple example of web service usage (here: currency exchange rate service from xmethods) in python via a http proxy. Proxy information is taken from the environment.

Python
 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
from SOAPpy import SOAPProxy
import os, sys
# python sample code for the Currency Exchange service


if len(sys.argv)<>3:
    print "usage: %s germany sweden\n\tyou may try other countries"%(sys.argv[0])
    sys.exit(-1)


# in some intranets an issue: how to use a web proxy for WS. Here
# we assume a set environment variable 'http_proxy'.·
# This is common in unix environments. SOAPpy does not like
# a leading 'http://'
if os.environ.has_key("http_proxy"):·
    my_http_proxy=os.environ["http_proxy"].replace("http://","")
else:
     my_http_proxy=None

url = 'http://services.xmethods.net:80/soap'
n   = 'urn:xmethods-CurrencyExchange'


server = SOAPProxy(url, namespace=n, http_proxy=my_http_proxy)

print sys.argv[1], sys.argv[2], server.getRate(sys.argv[1], sys.argv[2])

Discussion

SOAPpy makes not use of http_proxy from the environment directly and needs a specific format to work.

Sign in to comment