LuaXMLRPC笔记

XMLRPC


  XMLRPC 为以http为传输协议,使用xml格式化数据来执行远程过程调用, 区别于本地过程调用, 即发生在不同主机之间。

  属于分布式计算的一种简单实现,比web service简单易用。xml语言被多种语言广泛支持,是一种可扩展的标记语言,

xmlrpc被多种平台实现,以此提供的服务可以客户端和服务器端使用不同语言,

具有平台无关性,比socket发送网络字节序具有优势, 不用考虑大小端问题, 且报文内容遵循规定格式, 易于处理和阅读。

  此为UseLand公司制定的一套标准,http://xmlrpc.scripting.com/default.html, 角色分服务器端和客户端,
可以被不同语言实现,

c --- http://xmlrpc-c.sourceforge.net/

lua --- https://github.com/timn/lua-xmlrpc

php --- https://github.com/gggeek/phpxmlrpc/releases/tag/2.2.2

java --- http://ws.apache.org/xmlrpc/

luaXMLRPC


  luaXMLRPC是lua语言实现的一套软件包, 可以访问 和 提供 xmlrpc服务, 即服务器和客户端角色都支持。

https://github.com/timn/lua-xmlrpc

  

  客户端主要实现功能, 将lua table数据转换为 XML RPC message,

然后通过luaSocket库以http协议发送出去,

对于响应回来的XML RPC message 使用XML解析器 luaExpat 反向解析为 lua table,和返回值。

The http.lua file implements a simple stand-alone
client based on LuaSocket 2.0.2. The
following function is provided:


call (url, method [,
params])

Execute the call to method at
location url with the
given params (if any).
The method and params parameters
will be just passed to clEncode function.
The result is the same as clDecode function:
a boolean indicating whether the call was successful or not, followed by the
response value (if successful) or by the faultString and
the faultCode (if the call fails).

  服务器端, CGILUA接收到XML RPC message后, 使用luaExpat解析为lua table, 后调用相应方法,

将执行结果组装成XML RPC response message, 发送回客户端。

  服务器端注册方法接口:

srvMethods (tab_or_func)Register the methods
on the server. The parameter can be a table or a dispatching function. If
table is given it can have one level of objects with the
corresponding methods. If a function is given, it will
replace the dispatcher.

  luaXMLRPC提供的一些库函数,参见来自下载包帮助文档:

  

Library functions

The xmlrpc.lua file implements the functions that
encode and decode XML-RPC messages and transform data types between the Lua
and XML-RPC. The functions are:


clEncode (method_name [, params])
=> method_call

Build a XML-RPC document containing
methodCall element. It receives a string with the
method‘s name and an optional list of parameters. The result is a string
containing the XML-RPC document.

clDecode (method_response) =>
ok, results

Disassemble the server response into a Lua object. It receives a string
containing the XML-RPC document representing
the methodResponse element. The result is a boolean
indicating wether the call was successful or not followed by the resulting
objects (typically a methodResponse has only one value so only one Lua
object will be returned). In case of error
the false value is followed by the
XMLRPC faultString and the faultCode. This
values are extracted from the fault element.

srvDecode (method_call) =>
method_name, list_params

Disassemble the client request into a method‘s name and a table with the
list of parameters. It receives a string containing the XML-RPC document
representing the methodCall element. The result is a
string with the name of the method to be called and a Lua table with the
arguments to the call.

srvEncode (object, is_fault) =>
method_response

Build a XML-RPC document containing
methodResponse element. It receives a Lua object (a
number, a string, a table, a "created typed value" etc.) with the return
value of the call. The result is a string containing the XML-RPC document.
Note that XML-RPC defines that a response only
returns one value so a Lua function that returns more
than one value has to pack them into a table to guarantee
that all of them will be returned. The second parameter
(is_fault) can be used to force
fault element to be generated instead of
params. In this case, the Lua object must be a table
with the
members faultCode and faultString.

srvMethods
(tab_or_func)

Register the methods on the server. The parameter can be a table or a
dispatching function. If a table is given it can have one
level of objects with the corresponding methods. If
function is given, it will replace the dispatcher.

dispatch (method_name) =>
function

Returns a Lua function that implements the method call. Note that the
object is encapsulated into that function so that the call will be turned
into a real method call.

luaXMLRPC client +
phpXMLRPC server 实例


  php服务器端运行环境, 需要先行安装 xamp
软件包(https://www.apachefriends.org/zh_cn/index.html),

xamp为 x系统(包括 Linux windows mac) + a (Apache web server)+ m (MySql database)+
p (php server script interpretor),

为流行的php开发环境。

  phpXMLRPC服务器端,
xmlrpc还不是php的原始库,需要下载,下载地址 http://phpxmlrpc.sourceforge.net/。

下载后,解压后,将其的lib目录拷贝出来并命名为libphpxmlrpc, 拷贝到 php 的
xamp/php/pear(珍珠)目录。

使用下面博客中 加法
服务例子(http://blog.csdn.net/flyingfalcon/article/details/2229488),

在\xampp\htdocs目录下建立一个php文件 xmlrpc_server.php:


<?php

include ("libphpxmlrpc/xmlrpc.inc");
include ("libphpxmlrpc/xmlrpcs.inc");

if ($_SERVER[‘REQUEST_METHOD‘] != ‘POST‘)
exit(0);

$add_sig = array(array($xmlrpcString, $xmlrpcInt, $xmlrpcInt));
$add_doc = "Add the two integer together";

function add($params)
{
global $xmlrpcerruser;

$val = php_xmlrpc_decode($params);

$ret = $val[0] + $val[1];

return new xmlrpcresp(new xmlrpcval($ret, "int"));
}

$server = new xmlrpc_server(array(
"add" => array(
"function" => "add",
"signature" => $add_sig,
"docstring" => $add_doc
)));

?>

  luaXMLRPC 客户端,按照github下载的压缩包中的帮助文档, 将 src文件夹命名为 xmlrpc,

拷贝到 LUA_PATH 目录即可, 即与 lua.exe 平级。

Lua XML-RPC is composed by three Lua files:


init.lua

Main source file providing the API

http.lua

API to call XML-RPC via HTTP

server.lua

Server side API

These files should be copied to a directory
named xmlrpc created in
your LUA_PATH.

  luaXMLRPC客户端代码,放到test.lua中, 与lua.exe平级,功能 实现 加法 XMLRPC 请求,


require "xmlrpc.http"

local ok, res = xmlrpc.http.call ("http://127.0.0.1/xmlrpc_server.php", "add", 1, 2)

if not ok then
print ("not ok")
print("error="..res)
else
print ("ok")
print("sum ="..res);
end

  客户端运行结果

  

LuaXMLRPC笔记

时间: 2024-08-15 01:24:02

LuaXMLRPC笔记的相关文章

【安全牛学习笔记】

弱点扫描 ╋━━━━━━━━━━━━━━━━━━━━╋ ┃发现弱点                                ┃ ┃发现漏洞                                ┃ ┃  基于端口五福扫描结果版本信息(速度慢)┃ ┃  搜索已公开的漏洞数据库(数量大)      ┃ ┃  使用弱点扫描器实现漏洞管理            ┃ ╋━━━━━━━━━━━━━━━━━━━━╋ [email protected]:~# searchsploit Usage:

51CTO持续更新《通哥的运维笔记》

<通哥的运维笔记>将持续在51CTO网站更新,希望大家多多关注.互相学习,后期,我将会退出<通哥的运维笔记>系列视频教程,希望带给大家最大的收获,帮助大家更好的学习.进步.<通哥的运维笔记>主要从linux系统管理.虚拟化.cloudstack云平台以及网络管理之CCNA.CCNP.CCIE,等等方面深入讲解.

WPF笔记整理 - Bitmap和BitmapImage

项目中有图片处理的逻辑,因此要用到Bitmap.而WPF加载的一般都是BitmapImage.这里就需要将BitmapImage转成Bitmap 1. 图片的路径要用这样的,假设图片在project下的Images目录,文件名XXImage.png. pack://application:,,,/xxx;component/Images/XXImage.png 2. 代码: Bitmap bmp = null; var image = new BitmapImage(new Uri(this.X

java String 类 基础笔记

字符串是一个特殊的对象. 字符串一旦初始化就不可以被改变. String s = "abc";//存放于字符串常量池,产生1个对象 String s1=new String("abc");//堆内存中new创建了一个String对象,产生2个对象 String类中的equals比较字符串中的内容. 常用方法: 一:获取 1.获取字符串中字符的个数(长度):length();方法. 2.根据位置获取字符:charAt(int index); 3.根据字符获取在字符串中

vector 学习笔记

vector 使用练习: /**************************************** * File Name: vector.cpp * Author: sky0917 * Created Time: 2014年04月27日 11:07:33 ****************************************/ #include <iostream> #include <vector> using namespace std; int main

学习笔记之邮件发送篇

用脚本语言发送邮件是系统管理员必备技能 对系统定期检查或者当服务器受到攻击时生成文档和报表. 发布这些文档最快速有效的方法就是发送邮件. python中email模块使得处理邮件变得比较简单 发送邮件主要用到了smtplib和email两个模块,这里首先就两个模块进行一下简单的介绍: 本段摘录于    http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html 1.smtplib模块 smtplib.SMTP([host[, p

15.1-全栈Java笔记:Java事件模型是什么?事件控制的过程有哪几步??

应用前边两节上一章节的内容,大家可以完成一个简单的界面,但是没有任何的功能,界面完全是静态的,如果要实现具体功能的话,必须要学习事件模型. 事件模型简介及常见事件模型 对于采用了图形用户界面的程序来说,事件控制是非常重要的. 一个源(事件源)产生一个事件并把它(事件对象)送到一个或多个监听器那里,监听器只是简单地等待,直到它收到一个事件,一旦事件被接收,监听器将处理这些事件. 一个事件源必须注册监听器以便监听器可以接收关于一个特定事件的通知. 每种类型的事件都有其自己的注册方法,一般形式为: v

Java设计模式学习笔记,一:单例模式

开始学习Java的设计模式,因为做了很多年C语言,所以语言基础的学习很快,但是面向过程向面向对象的编程思想的转变还是需要耗费很多的代码量的.所有希望通过设计模式的学习,能更深入的学习. 把学习过程中的笔记,记录下来,只记干货. 第一部分:单例模式的内容 单例模式:类只能有一个实例. 类的特点:1.私有构造器:2.内部构造实例对象:3.对外提供获取唯一实例的public方法. 常见的单例模式实现有五种形式: 1.饿汉式. 2.懒汉式. 3.双重检查锁式. 4.静态内部类式. 5.枚举式. 以下分别

Caliburn.Micro学习笔记(一)----引导类和命名匹配规则

Caliburn.Micro学习笔记(一)----引导类和命名匹配规则 用了几天时间看了一下开源框架Caliburn.Micro 这是他源码的地址http://caliburnmicro.codeplex.com/ 文档也写的很详细,自己在看它的文档和代码时写了一些demo和笔记,还有它实现的原理记录一下 学习Caliburn.Micro要有MEF和MVVM的基础 先说一下他的命名规则和引导类 以后我会把Caliburn.Micro的 Actions IResult,IHandle ICondu