Ken MacLeod´Â Perl·Î XML-RPC¸¦ ±¸ÇöÇß´Ù. ±×ÀÇ À¥»çÀÌÆ®³ª CPANÀ» ÅëÇØ Frontier::RPC ¸ðµâÀ» ±¸ÇÒ ¼ö ÀÖ´Ù.
Frontier::RPC¸¦ ¼³Ä¡ÇÏ·Á¸é, ÆÐŰÁö¸¦ ³»·Á¹Þ°í PerlÀÇ Ç¥ÁØ ¹æ½ÄÀ¸·Î ÄÄÆÄÀÏÇÏ¸é µÈ´Ù:
bash$ gunzip -c Frontier-RPC-0.07b1.tar.gz | tar xvf - bash$ cd Frontier-RPC-0.07b1 bash$ perl Makefile.PL bash$ make bash$ make test bash$ su -c 'make install' |
(ÀÌ °úÁ¤Àº À©µµ¿ìÁ root °èÁ¤ÀÌ ¾Æ´Ñ °æ¿ì¿¡´Â Á¶±Ý ´Ù¸¦ ¼ö ÀÖ´Ù. ÀÚ¼¼ÇÑ °ÍÀº Perl ¹®¼¸¦ Âü°íÇ϶ó.)
´ÙÀ½ÀÇ ÇÁ·Î±×·¥Àº Perl·Î XML-RPC ¼¹ö¸¦ È£ÃâÇÏ´Â ¹æ¹ýÀ» º¸¿©ÁØ´Ù:
use Frontier::Client; # Make an object to represent the XML-RPC server. $server_url = 'http://xmlrpc-c.sourceforge.net/api/sample.php'; $server = Frontier::Client->new(url => $server_url); # Call the remote server and get our result. $result = $server->call('sample.sumAndDifference', 5, 3); $sum = $result->{'sum'}; $difference = $result->{'difference'}; print "Sum: $sum, Difference: $difference\n"; |
´ÙÀ½ÀÇ ÇÁ·Î±×·¥Àº Perl·Î XML-RPC ¼¹ö¸¦ ¸¸µå´Â ¹æ¹ýÀ» º¸¿©ÁØ´Ù:
use Frontier::Daemon; sub sumAndDifference { my ($x, $y) = @_; return {'sum' => $x + $y, 'difference' => $x - $y}; } # Call me as http://localhost:8080/RPC2 $methods = {'sample.sumAndDifference' => \&sumAndDifference}; Frontier::Daemon->new(LocalPort => 8080, methods => $methods) or die "Couldn't start HTTP server: $!"; |
Frontier::RPC2´Â CGI ±â¹ÝÀÇ ¼¹ö Áö¿ø ±â´ÉÀ» ³»ÀåÇϰí ÀÖÁö´Â ¾Ê´Ù. ÇÏÁö¸¸ ÇÊ¿äÇÑ ´ëºÎºÐ °ÍÀ» ±¸ÇöÇÒ ¼ö ÀÖ´Ù.
´ÙÀ½ÀÇ Äڵ带 À¥¼¹öÀÇ cgi-bin µð·ºÅ丮¿¡ sumAndDifference.cgi ¶ó´Â À̸§À¸·Î ÀúÀåÇ϶ó. (À¯´Ð½º ½Ã½ºÅÛ¿¡¼´Â chmod +x sumAndDifference.cgi·Î ½ÇÇà °¡´ÉÇϵµ·Ï ¸¸µé¾î ÁÖ¾î¾ß ÇÑ´Ù.)
#!/usr/bin/perl -w use strict; use Frontier::RPC2; sub sumAndDifference { my ($x, $y) = @_; return {'sum' => $x + $y, 'difference' => $x - $y}; } process_cgi_call({'sample.sumAndDifference' => \&sumAndDifference}); #========================================================================== # CGI Support #========================================================================== # Simple CGI support for Frontier::RPC2. You can copy this into your CGI # scripts verbatim, or you can package it into a library. # (Based on xmlrpc_cgi.c by Eric Kidd <http://xmlrpc-c.sourceforge.net/>.) # Process a CGI call. sub process_cgi_call ($) { my ($methods) = @_; # Get our CGI request information. my $method = $ENV{'REQUEST_METHOD'}; my $type = $ENV{'CONTENT_TYPE'}; my $length = $ENV{'CONTENT_LENGTH'}; # Perform some sanity checks. http_error(405, "Method Not Allowed") unless $method eq "POST"; http_error(400, "Bad Request") unless $type eq "text/xml"; http_error(411, "Length Required") unless $length > 0; # Fetch our body. my $body; my $count = read STDIN, $body, $length; http_error(400, "Bad Request") unless $count == $length; # Serve our request. my $coder = Frontier::RPC2->new; send_xml($coder->serve($body, $methods)); } # Send an HTTP error and exit. sub http_error ($$) { my ($code, $message) = @_; print <<"EOD"; Status: $code $message Content-type: text/html <title>$code $message</title> <h1>$code $message</h1> <p>Unexpected error processing XML-RPC request.</p> EOD exit 0; } # Send an XML document (but don't exit). sub send_xml ($) { my ($xml_string) = @_; my $length = length($xml_string); print <<"EOD"; Status: 200 OK Content-type: text/xml Content-length: $length EOD # We want precise control over whitespace here. print $xml_string; } |
ÀÌ À¯Æ¿¸®Æ¼ ·çƾÀ» CGI ½ºÅ©¸³Æ®¿¡ Áý¾î³ÖÀ¸¸é µÈ´Ù.