473,699 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xmlrpclib and methods declared at runtime

First off, I'm a python neophyte, but I'm fairly experienced with
Java, C and PHP.

I've been trying to use the xmlrpclib to perform remote calls against
a service, and it works nicely. However, due to my lack of
python-knowledge, I'm rather puzzled at the way the class works.

Specifically, suppose I have something like this snippet of code:

from xmlrpclib import ServerProxy

client = ServerProxy('ht tp://some_service')

client.system.l istMethods() #not declared in class
client.MyServic e.SomeOperation () #not declared in class
client.MyServic e.DoStuff() #not declared in class
Coming from mostly a java background, this is kind of freaky ;) How is
is the ServerProxy implementation doing this? That is, creating, or
perhaps binding is a better term, these additional functions at
runtime?

Based on having read the python tutorial and remembering the list of
built-in functions, my guess would have been calling setattr in the
constructor or after doing a listMethods operation against the XML-RPC
server.. That is, assuming the XML-RPC service supports this api..
something like that.. But that does not seem to be the case here, as a
search of xmlrpclib.py does not show ANY instances of setattr being
used. I did, however, notice some cases of classes implementing a
__getattr__ method, which leads me to the question of how the
interpreter performs lookup of methods when they are called. Does it
use getattr itself? Is it possible to, say, effectively override
getattr for a specific class, thereby returning a pointer to a generic
function that could be used to handle... well.. everything?

Regardless, if someone could explain this to me, or point me in the
direction of reading material, I'd be most grateful. I tried some
googling on stuff like "python reflection" but didn't come up with
anything that looked like this, and being a python neophyte I'm not
that up to speed on my code-comprehension yet, so trying to figure it
out on my own by reading the xmlrpclib.py would take aeons :) Since I
don't have any trouble using the lib, it's ok, but I like
understanding how stuff works, and this seems like a useful technique.

Regards,
squid.
Jul 25 '06 #1
2 1527
squid wrote:
>
Based on having read the python tutorial and remembering the list of
built-in functions, my guess would have been calling setattr in the
constructor or after doing a listMethods operation against the XML-RPC
server.. That is, assuming the XML-RPC service supports this api..
something like that.. But that does not seem to be the case here, as a
search of xmlrpclib.py does not show ANY instances of setattr being
used. I did, however, notice some cases of classes implementing a
__getattr__ method, which leads me to the question of how the
interpreter performs lookup of methods when they are called. Does it
use getattr itself? Is it possible to, say, effectively override
getattr for a specific class, thereby returning a pointer to a generic
function that could be used to handle... well.. everything?
You seem to have gotten it. When Python can't find an attribute in an
object's dict, it then looks for the magic __getattr__ method. If it's
present, it passes that method the name that you're looking for. This
lets you synthesize attributes and methods at runtime.
>>class Foo(object):
.... def __getattr__(sel f, name):
.... return name.upper()
....
>>f = Foo()
f.test
'TEST'

With xml-rpc, a method call on your side of the connection is
implemented by building up an XML message and sending it to the server
-- rather than statically try to figure out what oprations the server
supports, xmlrpclib will happily attempt to call any method name that
you can think of, and let the server tell you if there's an error.

It's probably not a good idea to let __getattr__ handle EVERYTHING your
class needs to do, but in cases like this where you don't know in
advance what the class will need to handle, it lets your code hide the
magic in a way that lets the users of your code forget that there's
anything magic going on at all. It just looks like code.

--
// Brett g Porter * bg*@bgporter.ne t
// http://www.bgporter.net/blog
Jul 26 '06 #2
On Wed, 26 Jul 2006 09:08:55 -0400, Brett g Porter <bg******@acm.o rg>
wrote:

<snip>

Brett,

Thanks a bunch for the explanation!

Regards,
squid.
Jul 26 '06 #3

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

Similar topics

0
1764
by: Larry | last post by:
I've had a production system running for a long time that uses xmlrpclib with timeoutsocket, and with my recent upgrade to 2.3 it's no longer able to use xmlrpclib with the xmlrpc servers I communicate with. I can call remote procedures still, but as soon as I import timeoutsocket and the old socket switcharoo is done it's over. To add to the fun I can use xmlrpc w/timeoutsocket if calling functions at the ubiquitous betty.userland.com...
0
1016
by: Roger Binns | last post by:
This was a fun problem to track down. The xmlrpclib._Method object should do something about __str__/__repr__. I have some code that prints out variables when an exception happens, and if anything references one of those _Method objects, then str/repr is called, which it tries to remote. The real problem arises when the original exception occurred doining some XML-RPC, so an exception is printed which when it hits the _Method object...
1
2671
by: Gabriel PASTOR | last post by:
I'm trying to send object using xmlrpclib, but it seems that classes inheriting from object cannot be marshalled. Here is an example: -------- server.py -------- import xmlrpclib, SimpleXMLRPCServer class MyObject(object): def __init__(self,name): self._name=name def getMyObject(name):
0
1119
by: Alan Little | last post by:
I'm trying to write a generic weblog update notifier using xmlrpclib, starting with technorati. What I want to do is something like this : XML config file that would look like this: <server url="http//whatever,technorati.org" methodname="technoratiMethod"> <param sequence="1" source="myAttrib1" /> <param sequence="2" source="myAttrib2" /> </server>
3
2780
by: Rune Froysa | last post by:
Trying something like:: import xmlrpclib svr = xmlrpclib.Server("http://127.0.0.1:8000") svr.test("\x1btest") Failes on the server with:: xml.parsers.expat.ExpatError: not well-formed (invalid token) (Smaller test-case: xmlrpclib.loads(xmlrpclib.dumps(('\x1btest',))))
0
1528
by: Willi Langenberger | last post by:
Hi! We have an application server (Zope) and make heavy use of xml-rpc. One problem arised, when we tried to return a zope.DateTime instance. xmlrpclib (naturally) knows nothing about zope.DateTime and marshalls it as instance, which gives a structure (of zope.DateTime attributes) on the client side (which isnt very usable). It seems we have two possibilities:
32
4512
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
5
2077
by: Jonathan Ballet | last post by:
Hello, I have developped a XMLRPC server, which runs under Gnu/Linux with python2.3. This server receives method calls from Windows client. The server got some parameters which are string, which contains carriage return characters, just after the line feed character; like "bla\n\rbla". The problem is, xmlrpclib "eats" those carriage return characters when
0
1653
by: Arno Stienen | last post by:
Perhaps I should be a bit more specific. When using this code to connect to a remote XML-RPC server (C++, xmlrpc++0.7 library): import xmlrpclib server = xmlrpclib.Server("http://10.10.101.62:29500") print server.Connection_Request("roberto") the Python command line 'hangs' until I kill the server. Then, the correct output is suddenly displayed:
0
8706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8630
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8944
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7786
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4638
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.