473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Solution for XML-RPC over a proxy

All,

I couldn't get my xml-rpc script to work via a corporate proxy.

I noticed a few posts asking about this, and a very good helper script by jjk on
starship. That script didn't work for me, and I think its a little old -- but it
was very helpful to figure it out.

The below script is a replacement/update to the earlier work. It is runnable as
a test or usable as a module. Tests pass from behind and away from a proxy, on
win32 and Linux i686, with Python 2.4.1.

Comments welcome.

Regards
Andrew

--- >8 --- >8 --- >8 --- >8 --- >8 --- >8 --- >8
#!/bin/env python
"""urllib2-based transport class for xmlrpclib.py (with test code).

Written from scratch but inspired by xmlrpc_urllib_transport.py
file from http://starship.python.net/crew/jjkunce/ by jjk.

A. Ellerton 2006-07-06

Testing with Python 2.4 on Windows and Linux, with/without a corporate
proxy in place.

****************************
*** USE AT YOUR OWN RISK ***
****************************
"""

import xmlrpclib

class ProxyTransport(xmlrpclib.Transport):
"""Provides an XMl-RPC transport routing via a http proxy.

This is done by using urllib2, which in turn uses the environment
varable http_proxy and whatever else it is built to use (e.g. the
windows registry).

NOTE: the environment variable http_proxy should be set correctly.
See checkProxySetting() below.

Written from scratch but inspired by xmlrpc_urllib_transport.py
file from http://starship.python.net/crew/jjkunce/ by jjk.

A. Ellerton 2006-07-06
"""

def request(self, host, handler, request_body, verbose):
import urllib2
self.verbose=verbose
url='http://'+host+handler
if self.verbose: "ProxyTransport URL: [%s]"%url

request = urllib2.Request(url)
request.add_data(request_body)
# Note: 'Host' and 'Content-Length' are added automatically
request.add_header("User-Agent", self.user_agent)
request.add_header("Content-Type", "text/xml") # Important

proxy_handler=urllib2.ProxyHandler()
opener=urllib2.build_opener(proxy_handler)
f=opener.open(request)
return(self.parse_response(f))
def checkProxySetting():
"""If the variable 'http_proxy' is set, it will most likely be in one
of these forms (not real host/ports):

proxyhost:8080
http://proxyhost:8080

urlllib2 seems to require it to have 'http;//" at the start.
This routine does that, and returns the transport for xmlrpc.
"""
import os, re
try:
http_proxy=os.environ['http_proxy']
except KeyError:
return

# ensure the proxy has the 'http://' at the start
#
match = re.match("(http://)?([-_\.A-Za-z]+):(\d+)", http_proxy)
if not match: raise Exception("Proxy format not recognised: [%s]" % http_proxy)
os.environ['http_proxy'] = "http://%s:%s" % (match.group(2), match.group(3))
#print "Determined proxy: %s" % os.environ['http_proxy']
return
def test():
import sys, os

def nextArg():
try: return sys.argv.pop(1)
except: return None

checkProxySetting()

url=nextArg() or "http://betty.userland.com"
api=nextArg() or "examples.getStateName(32)" # "examples.getStateList([1,2])"
try:
server=xmlrpclib.Server(url, transport=ProxyTransport())
print "Url: %s" % url

try: print "Proxy: %s" % os.environ['http_proxy']
except KeyError: print "Proxy: (Apparently none)"

print "API: %s" % api
r = eval("server.%s"%api)
print "Result: ", r

except xmlrpclib.ProtocolError, e:
print "Connection error: %s" % e
except xmlrpclib.Fault, e:
print "Error: %s" % e

if __name__=='__main__':
# run with no parameters for basic test case.
test()
--- >8 --- >8 --- >8 --- >8 --- >8 --- >8 --- >8

Jul 7 '06 #1
2 3278
Andrew R írta:
All,

I couldn't get my xml-rpc script to work via a corporate proxy.

I noticed a few posts asking about this, and a very good helper script by jjk on
starship. That script didn't work for me, and I think its a little old -- but it
was very helpful to figure it out.

The below script is a replacement/update to the earlier work. It is runnable as
a test or usable as a module. Tests pass from behind and away from a proxy, on
win32 and Linux i686, with Python 2.4.1.

Comments welcome.
I believe you should also post this in the Python Cookbook.
Jul 7 '06 #2
* Andrew R <an****@nospam.comwrote:
All,

I couldn't get my xml-rpc script to work via a corporate proxy.

I noticed a few posts asking about this, and a very good helper script by jjk on
starship. That script didn't work for me, and I think its a little old -- but it
was very helpful to figure it out.

The below script is a replacement/update to the earlier work. It is runnable as
a test or usable as a module. Tests pass from behind and away from a proxy, on
win32 and Linux i686, with Python 2.4.1.

Comments welcome.
No real comment, just to point out an alternative that works well for me:

http://docs.python.org/dev/lib/xmlrp...t-example.html
Stefan Krah
Jul 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom Frey | last post by:
Hi, is there any solution available for transforming WordML XML files to PDF on server requests. Here is basically what I'm looking for: We have like 15 different types of contracts that have...
1
by: lievemario | last post by:
Hey, I have generated following xml file (using perl,dbi), Now I would like to display this with the help of xsl such that I have the following result: A B (XXXX) (YYYY)
11
by: Todd | last post by:
I have been tasked to create an app that will be run from a CD on stand-alone machines that needs to search and retrieve data from an XML Data Island. The best approach that I can see to handle...
0
by: melledge | last post by:
XML Master Series - XML 2005 - November 14-18 - Hilton Hotel - Atlanta, GA IDEAlliance in its continued efforts to provide groundbreaking services to the XML community is introducing the XML...
6
by: Greg | last post by:
I am working on a project that will have about 500,000 records in an XML document. This document will need to be queried with XPath, and records will need to be updated. I was thinking about...
7
by: Scott M. Lyon | last post by:
Quick (hopefully easy) question for you guys. What is going to be the quickest/easiest way to generate XML from VB.NET? Note: I don't mean an XML file, but XML in memory somehow (an...
6
by: Chua Wen Ching | last post by:
Hi there, I have some questions to ask... just say i have this xml file: Scenario :- Script.xml ======== <software> <settings>
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
2
by: carlo.gherarducci | last post by:
Hi all, I'm a newbie in .net xml programming, so please be patient. And sorry for my uncorrect english, too. I'm going to explain my problem: I've built a web service which responds to ferries...
12
by: =?Utf-8?B?YW9pZmV5MTIzNA==?= | last post by:
I have a client who is using an asp page to receive xml files from my company. When the client views the xml through a browser it is formatted correctly i.e. indented properly etc. However, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.