header(-type=>'text') 在 Perl 中的 cgi 中打印什么

What does the header(-type=>'text') print in cgi in Perl

我是 cgi 的新手,正在尝试将 Perl cgi 脚本转换为 Python。

我在脚本中看到了这些语句。

my $q = new CGI;
print $q->header(-type=>'text');

通过查看它我可以理解 q 是一个标量变量,它拥有调用 CGI 脚本的表单数据。

但是上面的打印语句是做什么的呢?

它会检查 header 类型的值是否在 header 字典中,并据此打印 True 或 False 吗?

我没有 Perl 解释器来执行和检查输出。

开始输出

We use the CGI object to print out the HTTP header for the output:

print $q->header; 

This gives you the following HTTP header:

Content-Type: text/html; charset=ISO-8859-1 

If you need to have any extra options in your header, for example you may not want the default 'type', you can just pass them in to the header method:

print $q->header(-type => "text/plain"); 

This gives you the following HTTP header:

Content-Type: text/plain; charset=ISO-8859-1

有关详细信息,请访问 here

引用 CGI.pm documentation

header() returns the Content-type: header. You can provide your own MIME type if you choose, otherwise it defaults to text/html. An optional second parameter specifies the status code and a human-readable message. For example, you can specify 204, "No response" to create a script that tells the browser to do nothing at all. Note that RFC 2616 expects the human-readable phase to be there as well as the numeric status code.

print $cgi->header( -Content_length => 3002 );

"What does it do?"

my $q = new CGI 创建一个新的 https://metacpan.org/pod/CGI object,它可以为您做很多事情。最好的东西是参数、路径信息和 headers.

print $q->header(-type=>'text') 打印 header,它告诉浏览器正在生成什么。 -type=>'text' 不够;它应该 是一个完整的 MIME 类型,例如 text/plaintext/htmltext/csvapplication/json 或许多其他类型。您甚至可以生成图像并制作类型 image/jpeg 或其他内容。

它还会打印状态,即 200OK。你的程序 应该 在没有它的情况下工作,但添加它也没有坏处。

您正在尝试将此移植到 Python,我不知道 Python 必须使用哪些选项来编写此内容,但您可能不需要它。只需用换行符 (\n) 分隔您要放入的任何 header 条目,例如过期标签,然后添加另一个条目以将 header 与 body 分隔开。