-4

我只是想拥有一个休息 api 服务器,我可以调用它来通过 URL 更新文件,就是这样。

这是文件:

mytextfile:
       key1 = value1
       key2 = value2
  1. 在客户端上,将运行一个脚本,该脚本将一个或多个字符串发送到 API 服务器。
  2. 例如,API 服务器将接收它们/update.script?string1="blah"&string2="fun"(假装其 url 编码)
  3. 然后服务器应该解析这些字符串,然后调用 exec 函数,或者甚至在系统上执行一些 sed 命令来更新文件的另一个脚本

语言或实现并不重要。

寻找新鲜的想法。

所有建议表示赞赏。

4

2 回答 2

2

我不明白:您的问题/问题到底是什么?

我对“使用 url 编码的参数从 cgi 脚本内部修改文件”问题的方法是:

  1. 选择一种你喜欢的语言并开始编码,在我的例子中是 Perl。

    #!/usr/bin/perl
    use strict; use warnings;
    
  2. 获取你所有的论点。我将在这里使用 Perl 的CGI模块:

    use CGI::Carp;
    use CGI;
    my $cgi = CGI->new;
    
    # assuming we don't have multivalued fields:
    my %arguments = $cgi->Values; # handles (almost) *all* decoding and splitting
    # validate arguments
    # send back CGI header to acknowledge the request
    # the server will make a HTTP header from that
    
  3. 现在要么用它们调用一个特殊的子程序/函数……</p>

    updateHandler(%arguments);
    ...;
    my $filename = 'path to yer file name.txt';
    sub updateHandler {
       my %arguments = @_;
       # open yer file, loop over yer arguments, whatever
    
       # read in file
       open my $fileIn, '<', $filename or die "Can't open file for reading";
       my @lines = <$fileIn>;
       close $fileIn;
    
       # open the file for writing, completely ignoring concurrency issues:
       open my $fileOut, '>', $filename or die "Can't open file for writing";
    
       # loop over all lines, make substitutions, and print it out
       foreach my $line (@lines) {
          # assuming a file format with key-value pairs
          # keys start at the first column
          # and are seperated from values by an '=',
          # surrounded by any number of whitespace characters
          my ($key, $value) = split /\s*=\s*/, $line, 2;
    
          $value = $arguments{$key} // $value;
          # you might want to make sure $value ends with a newline
    
          print $fileOut $key, " = ", $value;
       }
    }
    

    不要使用这个相当不安全和次优的代码!我只是写这个来证明这并不复杂。

  4. ……或者想办法将你的参数发送到另一个脚本(尽管 Perl 非常适合文件操作任务)。根据您需要的脚本输出,选择或命令之一qw{},或者决定使用.systemexecopen my $fh, '|-', $commandopen

  5. 至于运行此脚本的服务器:Apache 对我来说看起来不错,除非您有非常特殊的需求(您自己的协议、单线程、低安全性、低性能),在这种情况下您可能想要编写自己的服务器。使用HTTP::Daemon模块,您可以为一个简单的服务器管理 <50 行。

    使用 Apache 时,我强烈建议使用 mod_rewrite 将其/path放入PATH_INFO环境变量中。当使用一个脚本来表示您的整个 REST API 时,您可以使用PATH_INFO选择许多方法/子例程/函数之一。这也消除了在 URL 中命名脚本的需要。

    比如转网址

    http://example.com/rest/modify/filename?key1=value1
    

    进入

    /cgi-bin/rest-handler.pl/modify/filename?key1=value1
    

    在 Perl 脚本中,我们将$ENV{PATH_INFO}包含/modify/filename.

这有点以 Perl 为中心,但只需选择您熟悉的任何语言并开始编码,利用您可以在途中使用的任何模块。

于 2012-08-17T02:04:44.103 回答
1

我会使用更新的 Perl 框架,比如Mojolicious。如果我制作一个文件(test.pl):

#!/usr/bin/env perl

use Mojolicious::Lite;
use Data::Dumper;

my $file = 'file.txt';

any '/' => sub {
  my $self = shift;
  my @params = $self->param;

  my $data = do $file;
  $data->{$_} = $self->param($_) for @params;

  open my $fh, '>', $file or die "Cannot open $file";

  local $Data::Dumper::Terse = 1;
  print $fh Dumper $data;

  $self->render( text => "File Updated\n" );
};

app->start;

然后运行morbo test.pl

并访问http://localhost:3000/?hello=world(或运行./test.pl get /?hello=world

然后我进去file.txt

{
  'hello' => 'world'
}

等等。

于 2012-08-17T03:20:07.487 回答