ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: SimpleXMLRPCServer niceties
Submitter: Rune Hansen (other recipes)
Last Updated: 2002/12/04
Version no: 1.1
Category:

 

Not Rated yet


Description:

Just thought I'd share a few xmlrpc server niceties.
- Simple (and not very secure) access controll
- Port rebinding (If you code anything like me, this comes in handy)
- register_instance with class

Source: Text Source

import SimpleXMLRPCServer
import string,socket

accessList=(
    '127.0.0.1',
    '192.168.0.15'
    )

class Server(SimpleXMLRPCServer.SimpleXMLRPCServer):
    def __init__(self,*args):
        SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self,(args[0],args[1]))
        
    def server_bind(self):
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        SimpleXMLRPCServer.SimpleXMLRPCServer.server_bind(self)

    def verify_request(self,request, client_address):
        if client_address[0] in accessList:
            return 1
        else:
            return 0
        
class xmlrpc_registers:
    def __init__(self):
        self.python_string = string
        
    def add(self, x, y):
        return x + y

    def mult(self,x,y):
        return x*y

    def div(self,x,y):
        return x//y

if __name__ == "__main__":
    server = Server('',8000)
    server.register_instance(xmlrpc_registers())
    server.serve_forever()

Discussion:



Add comment

Number of comments: 3

SimpleXMLRPCServer.SimpleXMLRPCServer, Jeff Bauer, 2003/01/17
One simplification. If you replace change the import statement to:

from SimpleXMLRPCServer import SimpleXMLRPCServer
Every "SimpleXMLRPCServer.SimpleXMLRPCServer" can be replaced with just "SimpleXMLRPCServer".
Add comment

CAUTION: Security advisory affects this code, David Ascher, 2005/02/03
As discussed in

  http://www.python.org/security/PSF-2005-001/
There is a new known vulnerability in the standard XML-RPC library in Python 2.2 and later. Patches are available off of that URL, and new ActivePython builds or instructions on how to fix existing installations will be available shortly. (Python.org folks are also planning on providing updated builds).

-- David Ascher
Add comment

Another small nicety on the client side, dirk janssen, 2006/10/13
Before you start any client or create a proxy, instruct the socket module to produce timeouts when connection is taking too long. Without this, your rpc call can hang indefinitely.

  socket.setdefaulttimeout(60)  
This is a time out in seconds, after which you will get a socket.error. You can easily catch this error and decide to try again, but at least your program will not hang forever.
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.