hello world_Hello World CGI

旧城等待, 2022-12-06 15:23 379阅读 0赞

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 )

  1. #!/usr/bin/perl
  2. print "Content-type: text/html\n\n";
  3. print <<HTML;
  4. <html>
  5. <head>
  6. <title>A Simple Perl CGI</title>
  7. </head>
  8. <body>
  9. <h1>A Simple Perl CGI</h1>
  10. <p>Hello World</p>
  11. </body>
  12. HTML
  13. 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:

关键是第一条印刷声明:

  1. 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.

在下一个示例中,让我们参与这个时间和日期脚本 ,并将其添加到您的网页中。

  1. #!/usr/bin/perl
  2. @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  3. @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
  4. ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
  5. $year = 1900 + $yearOffset;
  6. $theTime = "$weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
  7. print "Content-type: text/html\n\n";
  8. print <<HTML;
  9. <html>
  10. <head>
  11. <title>A Simple Perl CGI</title>
  12. </head>
  13. <body>
  14. <h1>A Simple Perl CGI</h1>
  15. <p>$theTime</p>
  16. </body>
  17. HTML
  18. 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

发表评论

表情:
评论列表 (有 0 条评论,379人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Hello World

    Hello World 一、简述          简单的Hello World程序。(时间久了就会忘,趁着还有印象先记下)     1、C语言:  控制台程序、有窗体

    相关 Hello World

    这是我的第一篇博客,虽然是第一次写博客,但是之前也在微信公众号上写过一些文章(虽然没有阅读量),或多或少对自己产生了一定的帮助。 这些天会考虑将其中一些较为有意义的