使用LWP :: Agent的Perl JSON :: RPC :: Client

使用LWP :: Agent的Perl JSON :: RPC :: Client,第1张

概述我被要求不使用 JSON :: RPC :: Client,而是使用LWP来进行调用. 这是我的代码: 服务器: #!/usr/bin/perluse strict;use lib ".";use ServerLib;use JSON::RPC::Server::Daemon;die "Do Not run as Root\n" if($< == 0);print "Start 我被要求不使用 JSON :: RPC :: ClIEnt,而是使用LWP来进行调用.

这是我的代码:

服务器:

#!/usr/bin/perluse strict;use lib ".";use Serverlib;use JsON::RPC::Server::Daemon;dIE "Do Not run as Root\n" if($< == 0);print "Starting Daemon\n";my $daemon =  JsON::RPC::Server::Daemon->new(LocalPort => 8000);$daemon->dispatch({'/Jsonrpc/API' => 'Serverlib'})->handle();exit;

模块:

package Serverlib;use base qw(JsON::RPC::Procedure); # Perl 5.6 or more thanuse JsON;use Data::Dumper;use Sys::Hostname;use Net::RabbitMQ;use YAML qw( Loadfile );use strict;$| = 1;my $VERSION = 0.2.0;sub echo : Public(message:string) {    my ($s,$obj) = @_;    my $message = $obj->{'message'};    print "got message => $message\n";    print "returning: $message\n";    my $object = { message => $message,foobar => "garbage" };    return $object; }package Serverlib::system;sub describe {    {        sdversion => "0.1",name      => 'Serverlib',};}1;

工作客户:

#!/usr/bin/perluse strict;use JsON::RPC::ClIEnt;use Data::Dumper;my $clIEnt = new JsON::RPC::ClIEnt;my $uri = 'http://localhost:8000/Jsonrpc/API';my $obj = {    method  => 'echo',# or 'MyApp.sum'    params  => ["my message"],};my $res = $clIEnt->call( $uri,$obj );if($res){   if ($res->is_error) {       print "Error : ",$res->error_message;   }    else {       print Dumper($res->result);   } } else {    print $clIEnt->status_line; }

非工作客户:

#!/usr/bin/perl -wuse strict;use JsON;use LWP::Simple;use Data::Dumper;my $actionurl = "http://localhost:8000/Jsonrpc/API";my $ua = LWP::UserAgent->new();$ua->agent("JsONClIEnt/0.1");my $object = { method => "echo",params => [@ARGV ] };my $Json = to_Json($object);print "$Json\n";my $req = http::Request->new(POST => $actionurl);$req->content_type('application/Json');$req->content($Json);my $res = $ua->request($req);if ($res->is_success){    print "Succeeded:\n" . Dumper($res->dump);    print "DUmp: ". $res->dump;    my $result = to_Json($res->content); }  else      {        print "Failed\n"; }

我看到两个客户端的服务器进程,但第二个客户端没有返回给它的数据.

DUmp: http/1.1 200 OKConnection: closeDate: Tue,03 Jan 2012 18:24:24 GMTServer: libwww-perl-daemon/5.827Content-Type: application/Json; charset=UTF-8ClIEnt-Date: Tue,03 Jan 2012 18:24:24 GMTClIEnt-Peer: 127.0.0.1:8000ClIEnt-Response-Num: 1(no content)

有人看到我错过了吗?它应该很简单,但由于某种原因,我找不到像RPC :: ClIEnt看到的响应字符串.

解决方法 这是一个棘手的问题.我的一个同事找到了它并且他不在Stackoverflow上,所以我将发布修复程序.

这个:

my $object = { method => "echo",params => [@ARGV ] };

需要的版本=> “1.1”,加入它成为:

my $object = { method => "echo",params => [@ARGV],version => "1.1",};

不知道为什么这很重要,但一旦它被添加它就像一个魅力.

总结

以上是内存溢出为你收集整理的使用LWP :: Agent的Perl JSON :: RPC :: Client全部内容,希望文章能够帮你解决使用LWP :: Agent的Perl JSON :: RPC :: Client所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1236317.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存