hello world_Hello World CGI
hello world
A CGI script can be as simple or complex as you need it to be. It could be in Perl, Java, Python or any programming language. At its core, a CGI application simply takes a request via HTTP (typically a web browser) and returns HTML. Let’s look at a simple Perl Hello World CGI script and break it down into it’s simplest forms.
CGI脚本可以根据需要简单或复杂。 它可以是Perl, Java ,Python或任何编程语言 。 CGI应用程序的核心只是通过HTTP(通常是Web浏览器)接收请求并返回HTML。 让我们看一个简单的Perl Hello World CGI脚本,并将其分解成最简单的形式。
“ Hello World” CGI Perl脚本 ( ‘Hello World’ CGI Perl Script )
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>A Simple Perl CGI</title>
</head>
<body>
<h1>A Simple Perl CGI</h1>
<p>Hello World</p>
</body>
HTML
exit;
If you run the program on the command line, you’ll see that it does exactly what you’d expect. First, it prints the Content-type line, then it prints the raw HTML. In order to see it in action in a web browser, you’ll need to copy or upload the script to your web server and make sure the permissions are set correctly (chmod 755 on *nix systems). Once you’ve set it correctly, you should be able to browse to it and see the page displayed live on your server.
如果您在命令行上运行该程序,则会看到它完全符合您的期望。 首先,它打印Content-type行,然后打印原始HTML。 为了在Web浏览器中看到它的运行效果,您需要将脚本复制或上传到Web服务器,并确保正确设置了权限(* nix系统上的chmod 755)。 正确设置后,您应该能够浏览到它并看到服务器上实时显示的页面。
The key line is the first print statement:
关键是第一条印刷声明:
print "Content-type: text/html\n\n";
This tells the browser that the document coming after the two newlines is going to be HTML. You must send a header so the browser knows what type of document is coming next, and you must include a blank line between the header and the actual document.
这告诉浏览器,两个换行符之后的文档将是HTML。 您必须发送标题,以便浏览器知道接下来要发送的文档类型,并且标题和实际文档之间必须包含空白行。
Once the header is sent, it’s just a matter of sending the HTML document itself. In the above example, we’re using a here-doc to simplify printing a large chunk of plain text. Of course, this is really no different than having a plain HTML document sitting on your server. The real power of using a programming language like Perl to create your HTML comes when you add in some fancy Perl programming.
发送标头后,只需发送HTML文档本身即可。 在上面的示例中,我们使用here-doc简化了打印大量纯文本的过程。 当然,这与在服务器上放置纯HTML文档没有什么不同。 当您添加一些精美的Perl编程时,使用诸如Perl这样的编程语言来创建HTML的真正力量就在于。
添加到基本脚本 ( Adding on to the Basic Script )
In the next example, let’s take part of this time and date script and add it to your web page.
在下一个示例中,让我们参与这个时间和日期脚本 ,并将其添加到您的网页中。
#!/usr/bin/perl
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
$theTime = "$weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>A Simple Perl CGI</title>
</head>
<body>
<h1>A Simple Perl CGI</h1>
<p>$theTime</p>
</body>
HTML
exit;
This new CGI script will insert the current date into the page each time the script is called. In other words, it becomes a dynamic document that changes as the date changes, rather than a static document.
每次调用该脚本时,此新的CGI脚本都会将当前日期插入页面。 换句话说,它变成了动态文档,随着日期的变化而变化,而不是静态文档。
翻译自: https://www.thoughtco.com/hello-world-cgi-2641144
hello world
还没有评论,来说两句吧...