如何通过命令行将 POST 和 GET 数据发送到 Perl CGI 脚本?
我正在尝试通过命令行参数发送 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
要从命令行测试 CGI 程序,您需要伪造服务器为该程序创建的环境。 CGI.pm 有一个特殊的离线模式,但我经常发现不使用它更容易,因为我需要为程序通常期望的其他所有内容进行额外的设置。
根据脚本的实现,这涉及设置许多环境变量,您可以通过伪装成服务器的包装器脚本来执行此操作:
如果您针对 POST 请求执行此操作,则环境会略有不同,您需要在标准输入上提供 POST 数据:
您可以根据需要将其设置为奇特,并且每次要测试程序时,都可以更改 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:
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:
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.
您使用的是标准 CGI 模块吗?
例如,对于以下程序(请注意
use CGI
的参数中的-debug
),您可以在命令行上为其提供参数:
您也可以通过标准输入来执行此操作:
Are you using the standard CGI module?
For example, with the following program (notice
-debug
in the arguments touse CGI
)you feed it parameters on the command line:
You can also do so via the standard input:
旧的讨论,但我一直在寻找相同的答案 - 所以对于那些关注的人 - 这就是我发现
RTFM!来自 CGI 手册页(还有更多)
调试
如果您从命令行或在 perl 中运行脚本
调试器,您可以向脚本传递关键字列表或参数=值
在命令行上或从标准输入中配对(您不必
担心欺骗你的脚本从环境中读取
变量)。您可以像这样传递关键字:
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:
如果您不想更改 perl 脚本,则可以使用至少两个设置的环境变量来调用它,正如其他人已经提到的那样。模拟 GET 请求:
这是 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:
That's the console shortcut for www.myserver.org/events_html.pl?limit=20
是的,可以从命令行绕过服务器来执行此操作。此页面解释了所有内容:Perl CGI 调试 (sitewizard.com)(特别是第 6 项该页面)。这里我引用最重要的部分:
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:
为 cgi 脚本提供发布数据:
为 cgi 脚本提供获取数据:
To give a cgi script post data:
To give a cgi script get data:
LWP 附带可以从命令行使用的现成脚本。检查系统中的
GET
和POST
脚本。LWP comes with ready made scripts that can be used from the command-line. Check for
GET
andPOST
scripts in your system.在 Windows 中,您可以使用 VBScript 编写调用 MS XML 库的命令行实用程序:
编辑:此脚本发送 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:
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.