如何通过命令行将 POST 和 GET 数据发送到 Perl CGI 脚本?

发布于 2024-08-20 07:17:32 字数 146 浏览 4 评论 0原文

我正在尝试通过命令行参数发送 get 或 post。那是在通过浏览器测试之前在命令行中测试脚本(服务器有问题)。我尝试在网上搜索,我想我可能使用了不正确的术语,因为我什么也没得到。我知道这是可能的,因为我看到有人这样做。我只是不记得它是如何完成的。

谢谢! :)

I am trying to send a get or a post through a command-line argument. That is test the script in the command line before I test through a browser (the server has issues). I tried searching online, and I suppose I was probably using incorrect terminology because I got nothing. I know this is possible because I saw someone do it. I just don't remember how it was done.

Thanks! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浅忆流年 2024-08-27 07:17:32

要从命令行测试 CGI 程序,您需要伪造服务器为该程序创建的环境。 CGI.pm 有一个特殊的离线模式,但我经常发现不使用它更容易,因为我需要为程序通常期望的其他所有内容进行额外的设置。

根据脚本的实现,这涉及设置许多环境变量,您可以通过伪装成服务器的包装器脚本来执行此操作:

 #!/bin/bash

 export HTTP_COOKIE=...
 export HTTP_HOST=test.example.com
 export HTTP_REFERER=...
 export HTTP_USER_AGENT=...
 export PATH_INFO=
 export QUERY_STRING=$(cat query_string);
 export REQUEST_METHOD=GET

 perl program.cgi

如果您针对 POST 请求执行此操作,则环境会略有不同,您需要在标准输入上提供 POST 数据:

 #!/bin/bash

 export CONTENT_LENGTH=$(perl -e "print -s q/post_data/");
 export HTTP_COOKIE=...
 export HTTP_HOST=test.example.com
 export HTTP_REFERER=...
 export HTTP_USER_AGENT=...
 export PATH_INFO=...
 export QUERY_STRING=$(cat query_string);
 export REQUEST_METHOD=POST

 perl program.cgi < post_data

您可以根据需要将其设置为奇特,并且每次要测试程序时,都可以更改 query_string 或 post_data 文件中的数据。如果您不想在 shell 脚本中执行此操作,则制作包装 Perl 脚本也同样容易。

To test a CGI program from the command line, you fake the environment that the server creates for the program. CGI.pm has a special offline mode, but often I find it easier not to use because of the extra setup I need to do for everything else my programs typically expect.

Depending on the implementation of your script, this involves setting many environment variables, which you can do from a wrapper script that pretends to be the server:

 #!/bin/bash

 export HTTP_COOKIE=...
 export HTTP_HOST=test.example.com
 export HTTP_REFERER=...
 export HTTP_USER_AGENT=...
 export PATH_INFO=
 export QUERY_STRING=$(cat query_string);
 export REQUEST_METHOD=GET

 perl program.cgi

If you're doing this for a POST request, the environment is slightly different and you need to supply the POST data on standard input:

 #!/bin/bash

 export CONTENT_LENGTH=$(perl -e "print -s q/post_data/");
 export HTTP_COOKIE=...
 export HTTP_HOST=test.example.com
 export HTTP_REFERER=...
 export HTTP_USER_AGENT=...
 export PATH_INFO=...
 export QUERY_STRING=$(cat query_string);
 export REQUEST_METHOD=POST

 perl program.cgi < post_data

You can make this as fancy as you need and each time you want to test the program, you change up the data in the query_string or post_data files. If you don't want to do this in a shell script, it's just as easy to make a wrapper Perl script.

筑梦 2024-08-27 07:17:32

您使用的是标准 CGI 模块吗?

例如,对于以下程序(请注意 use CGI 的参数中的 -debug),

#! /usr/bin/perl

use warnings;
use strict;

use CGI qw/ :standard -debug /;

print "Content-type: text/plain\n\n",
      map { $_ . " => " . param($_) . "\n" }
      param;

您可以在命令行上为其提供参数:

$ ./prog.cgi foo=bar baz=quux
Content-type: text/plain

foo => bar
baz => quux

您也可以通过标准输入来执行此操作:

$ ./prog.cgi
(offline mode: enter name=value pairs on standard input; press ^D or ^Z when done)
foo=bar
baz=quux
^D
Content-type: text/plain

foo => bar
baz => quux

Are you using the standard CGI module?

For example, with the following program (notice -debug in the arguments to use CGI)

#! /usr/bin/perl

use warnings;
use strict;

use CGI qw/ :standard -debug /;

print "Content-type: text/plain\n\n",
      map { $_ . " => " . param($_) . "\n" }
      param;

you feed it parameters on the command line:

$ ./prog.cgi foo=bar baz=quux
Content-type: text/plain

foo => bar
baz => quux

You can also do so via the standard input:

$ ./prog.cgi
(offline mode: enter name=value pairs on standard input; press ^D or ^Z when done)
foo=bar
baz=quux
^D
Content-type: text/plain

foo => bar
baz => quux
中性美 2024-08-27 07:17:32

旧的讨论,但我一直在寻找相同的答案 - 所以对于那些关注的人 - 这就是我发现

RTFM!来自 CGI 手册页(还有更多)
调试
如果您从命令行或在 perl 中运行脚本
调试器,您可以向脚本传递关键字列表或参数=值
在命令行上或从标准输入中配对(您不必
担心欺骗你的脚本从环境中读取
变量)。您可以像这样传递关键字:

    your_script.pl keyword1 keyword2 keyword3

or this:

   your_script.pl keyword1+keyword2+keyword3

or this:

    your_script.pl name1=value1 name2=value2

or this:

    your_script.pl name1=value1&name2=value2

To turn off this feature, use the -no_debug pragma.

Old discussion, but I was looking for the same answers - so for those who follow - this is what I found out

RTFM! from the CGI man page ( and there is more )
DEBUGGING
If you are running the script from the command line or in the perl
debugger, you can pass the script a list of keywords or parameter=value
pairs on the command line or from standard input (you don't have to
worry about tricking your script into reading from environment
variables). You can pass keywords like this:

    your_script.pl keyword1 keyword2 keyword3

or this:

   your_script.pl keyword1+keyword2+keyword3

or this:

    your_script.pl name1=value1 name2=value2

or this:

    your_script.pl name1=value1&name2=value2

To turn off this feature, use the -no_debug pragma.
暗藏城府 2024-08-27 07:17:32

如果您不想更改 perl 脚本,则可以使用至少两个设置的环境变量来调用它,正如其他人已经提到的那样。模拟 GET 请求:

shell$ QUERY_STRING=limit=20 REQUEST_METHOD=GET ./events_html.pl

这是 www.myserver.org/events_html.pl?limit=20 的控制台快捷方式

If you don't want to alter the perl script, you can call it with at least two environment variables set, as others mentioned already. To simulate a GET request:

shell$ QUERY_STRING=limit=20 REQUEST_METHOD=GET ./events_html.pl

That's the console shortcut for www.myserver.org/events_html.pl?limit=20

安稳善良 2024-08-27 07:17:32

是的,可以从命令行绕过服务器来执行此操作。此页面解释了所有内容:Perl CGI 调试 (sitewizard.com)(特别是第 6 项该页面)。这里我引用最重要的部分:

使用以下命令离线测试脚本
GET方法,只需设置
QUERY_STRING 环境变量
因此。如果您使用的是 Windows,
你可以使用以下命令
运行前在 DOS 窗口中输入一行
同一窗口中的脚本:

设置[电子邮件受保护]&全名=M+名称< /p>

使用以下命令离线测试脚本
POST 方法,将下面的行放入
名为 testinput.txt 的文本文件。

[电子邮件受保护]&全名=M+姓名

然后将该文件重定向为输入
脚本。在 Unix 系统上以及
在 Windows 的 MSDOS 提示符下,您可以
这样做:

perl -w 脚本名.pl <测试输入.txt

然后您的脚本将收到该信息
输入就好像它是由发送的一样
网站上的表格。检查错误
Perl 发出的消息(如果有的话)
帮助您跟踪问题
脚本。

Yes, it's possible to do this from the command line, bypassing your server. This page explains all: Perl CGI debugging (sitewizard.com) (Especially item 6 on that page). Here I quote the most important part:

To test the script offline using the
GET method, simply set the
QUERY_STRING environment variable
accordingly. If you are using Windows,
you might use the following command
line in a DOS window prior to running
the script in the same window:

set [email protected]&Fullname=M+Name

To test the script offline using the
POST method, put the line below into a
text file named, say, testinput.txt.

[email protected]&Fullname=M+Name

Then redirect that file as an input to
the script. On Unix systems as well as
under Windows' MSDOS prompt, you can
do it this way:

perl -w scriptname.pl < testinput.txt

Your script will then receive that
input as though it was sent it by a
form on the website. Check the error
messages that perl spouts, if any, to
help you track the problem in the
script.

罗罗贝儿 2024-08-27 07:17:32

为 cgi 脚本提供发布数据:

$ echo -n 'a=b;c=d' | REQUEST_METHOD=POST CONTENT_LENGTH=999 perl index.cgi

为 cgi 脚本提供获取数据:

$ perl index.cgi 'a=b;c=d'

To give a cgi script post data:

$ echo -n 'a=b;c=d' | REQUEST_METHOD=POST CONTENT_LENGTH=999 perl index.cgi

To give a cgi script get data:

$ perl index.cgi 'a=b;c=d'
动听の歌 2024-08-27 07:17:32

LWP 附带可以从命令行使用的现成脚本。检查系统中的 GETPOST 脚本。

LWP comes with ready made scripts that can be used from the command-line. Check for GET and POST scripts in your system.

东风软 2024-08-27 07:17:32

在 Windows 中,您可以使用 VBScript 编写调用 MS XML 库的命令行实用程序:

Dim XMLHttp : Set XMLHttp = CreateObject("Microsoft.XMLHTTP")
On Error Resume Next

strIPAddress = WScript.Arguments(0)
strMACAddress = WScript.Arguments(1)
strSubnetMask = WScript.Arguments(2)

On Error Goto 0

WScript.Echo "Attempting to wake host " & strIPAddress & " on NIC " & strMACAddress &
"using netmask " & strSubnetMask

strGetUrl = http://wolService/WolService/WolService.asmx/WakeBroadcast?hostIP=" &
strIPAddress & "&macAddress=" & strMACAddress & "&subnetMask=" & strSubnetMask

XMLHttp.Open "GET", strGetUrl, False
XMLHttp.Send ""

WScript.Echo XMLHttp.ResponseText

编辑:此脚本发送 HTTP 请求,并且可以从命令行使用。我对“如何通过命令行将 POST 和 GET 数据发送到 Perl CGI 脚本”这个问题感到困惑,并认为这是关于通过命令行从未指定的客户端操作系统将 POST 和 GET 数据发送到 Perl CGI 脚本。

In Windows, you can use VBScript to write a command line util that calls into the MS XML library:

Dim XMLHttp : Set XMLHttp = CreateObject("Microsoft.XMLHTTP")
On Error Resume Next

strIPAddress = WScript.Arguments(0)
strMACAddress = WScript.Arguments(1)
strSubnetMask = WScript.Arguments(2)

On Error Goto 0

WScript.Echo "Attempting to wake host " & strIPAddress & " on NIC " & strMACAddress &
"using netmask " & strSubnetMask

strGetUrl = http://wolService/WolService/WolService.asmx/WakeBroadcast?hostIP=" &
strIPAddress & "&macAddress=" & strMACAddress & "&subnetMask=" & strSubnetMask

XMLHttp.Open "GET", strGetUrl, False
XMLHttp.Send ""

WScript.Echo XMLHttp.ResponseText

Edit: This script sends HTTP requests and can be used from the command line. I got confused by the question 'How can I send POST and GET data to a Perl CGI script via the command line' and thought this was about sending POST and GET data to a Perl CGI script via the command line from an unspecified client OS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文