我目前正在使用SOAP::Lite来研究perl和SOAP。
我有一个简单的SOAP服务器,看起来运行良好:
#!perl -w
use SOAP::Transport::HTTP;
use Demo;
# don't want to die on 'Broken pipe' or Ctrl-C
$SIG{PIPE} = $SIG{INT} = 'IGNORE';
my $daemon = SOAP::Transport::HTTP::Daemon
-> new (LocalPort => 801)
-> dispatch_to('/home/soaplite/modules')
;
print "Contact to SOAP server at ", $daemon->url, "\n";
$daemon->handle;它包含一个名为Demo的小类,它只是检索系统的总内存:
Demo.py
#!/usr/bin/perl
use Sys::MemInfo qw(totalmem freemem totalswap);
print "total memory: ".(&totalmem / 1024)."\n";我下面有一个用PERL编写的SOAP客户端的例子,尽管我不确定如何与服务器通信(因为我在这里遵循的tutorial是切线的,例如从客户端检索Demo.py类的结果:
#!perl -w
use SOAP::Lite;
# Frontier http://www.userland.com/
$s = SOAP::Lite
-> uri('/examples')
-> on_action(sub { sprintf '"%s"', shift })
-> proxy('http://superhonker.userland.com/')
;
print $s->getStateName(SOAP::Data->name(statenum => 25))->result; 任何帮助都将不胜感激:)
发布于 2011-09-24 21:43:10
对于服务器脚本,dispatch_to方法采用要加载的包的路径以及包本身的名称。如果您传递第三个参数,它将限制服务器可见的方法的名称。(例如,两个名为memory和time的方法,将Demo::time作为第三个参数传递将使memory对客户端服务不可见。)
文件server.pl
my $daemon = SOAP::Transport::HTTP::Daemon
-> new (LocalPort => 801)
-> dispatch_to('/home/soaplite/modules', 'Demo')
;您的Demo包应该是一个包含返回值的方法的包。我无法在我的系统上编译Sys::MemInfo,所以我只使用localtime。我不知道为什么您将包命名为Demo.py,但是Perl包必须具有扩展名pm,否则它们将无法正确加载。
文件Demo.pm
#!/usr/bin/perl
package Demo;
#use Sys::MemInfo qw(totalmem freemem totalswap);
sub memory {
#print "total memory: ".(&totalmem / 1024)."\n";
return "Can't load Sys::MemInfo, sorry";
}
sub time {
my $time = localtime;
return $time;
}
1;对于客户端代码,必须正确指定两个重要部分才能正常工作,即proxy和uri。代理是到soap web服务的url路径。由于您将服务器脚本作为守护进程运行,因此您的路径只是网站的url。我的电脑没有网址,所以我用了http://localhost:801/。801是您在上面指定的端口。如果您在不同的web服务器(如Apache)中作为cgi脚本运行,则需要指定要调用的cgi脚本(例如http://localhost/cgi-bin/server.pl,将server.pl中的包更改为SOAP::Transport::HTTP::CGI。
uri可能是最令人困惑的,但它是web服务返回的xml文件的名称空间。启用+trace => 'debug'可查看web服务返回的xml文件。uri应该只是服务器的名称。即使您切换端口或使用cgi分派方法,此uri也保持不变。
文件test.pl
#!perl -w
use SOAP::Lite +trace => 'debug';
# Frontier http://www.userland.com/
$s = SOAP::Lite->new(proxy => 'http://superhonker.userland.com:801/',
uri => 'http://superhonker.userland.com/');
#might be http://www.userland.com/
#but I could not test sub-domains
print $s->time()->result;发布于 2011-09-21 05:25:57
https://stackoverflow.com/questions/7488748
复制相似问题