Ejemplos de XML-RPC

Servidores XML-RPC

Ejemplo 1 (Python)


server.py

from SimpleXMLRPCServer import *
 
class MisFunc:
    def fact(self,n):
        if n==0: return 1
        else: return n*self.fact(n-1)
 
def comb(a,b,c):
    return a/(b*c)
 
server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(comb, 'comb')
server.register_introspection_functions()
server.register_instance(MisFunc())
server.serve_forever()

Ejemplo 2 (Ruby)


server.rb

#!/usr/bin/ruby
 
require 'webrick'
require 'xmlrpc/server'
 
s = XMLRPC::WEBrickServlet.new
 
s.add_handler("math.fact") do |n|
  r = 1
  n.times do |i|
    r=r*(i+1)
  end
  r
end
 
s.add_handler("math.comb") do |a,b,c|
  if b == 0 or c ==0
    raise XMLRPC::FaultException.new(1, "divicion por cero")
  else
    a / (b*c)
  end
end
 
s.set_default_handler do |name, *args|
  raise XMLRPC::FaultException.new(-99, "Metodo #{name} no existe" +
                                   " o nuemero de parametros erroneos!")
end
 
server = WEBrick::HTTPServer.new(:Port => 9876)
server.mount("/RPC2", s)
trap("INT"){ server.shutdown }
server.start

Ejemplo 3 (Scheme)


server.scm

#!/usr/bin/mzscheme -f
(require (lib "xmlrpc.ss" "xmlrpc"))
 
; Demo de xml-rpc usando CGI
(xmlrpc-handlers
 (define (fact n)
   (if (= n 0) 1 (* n (fact (- n 1))))
   )
 
 (define (comb a b c)
   (/ a (* b c))
   )
 
 (handler 'math.fact fact)
 (handler 'math.comb comb)
 )

Clientes XML-RPC

Ejemplo 4 (Python)


cliente.py

import xmlrpclib
 
server = xmlrpclib.Server("http://localhost:8000")
#server = xmlrpclib.Server("http://localhost:9876/RPC2")
 
def calcComb(a,b):
    fa=server.fact(a)
    fab=server.fact(a-b)
    fb=server.fact(b)
    res=server.comb(fa,fab,fb)
    return res
 
for i in range(0,5):
    print " "*(5-i),
    for j in range(0,i+1):
        print calcComb(i,j),
    print

Ejemplo 5 (Ruby)


cliente.rb

require 'xmlrpc/client'
require 'pp'
 
server = XMLRPC::Client.new2("http://xmlrpc-c.sourceforge.net/api/sample.php")
result = server.call("sample.sumAndDifference", 5, 3)
pp result

Ejemplo 6 (Scheme)


cliente.scm

(require (lib "xmlrpc.ss" "xmlrpc"))
(read-case-sensitive #t)
 
(define server (xml-rpc-server "http://localhost/~abuss/lableng2/xmlrpc/server.scm"))
 
(define fac (server 'math.fact))
 
(define comb (server 'math.comb))
 
(fac 5)
(comb 6 3 4)
 
materias/laboratorio_de_lenguajes_ii/lableng2/ejemplosxmlrpc.txt · Última modificación: 2011/01/24 15:30 (editor externo)
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki