A brief introduction to the CGI programming _python in Ruby

Source: Internet
Author: User
Tags html form


Ruby is a common language, not just a language applied to web development, but Ruby's development in Web applications and Web tools is the most common.



With Ruby you can not only write your own SMTP server, FTP program, or ruby Web server, but you can also use Ruby for CGI programming.



Next, let's take a moment to the school Ruby CGI editor.
writing a CGI script



The most scripted Ruby CGI code looks like this:



#!/usr/bin/ruby puts "http/1.0 OK" puts "content-type:text/html\n\n" puts "This is a test"



You can keep the code in the test.cgi file, last to the server and given sufficient permissions to execute as a CGI script.



If your station address is http://www.example.com/, you can access the program using HTTP://WWW.EXAMPLE.COM/TEST.CGI, and the output will be: "This is a test.".



When the browser accesses the URL, the Web server finds the test.cgi file in the site directory and then parses the script code through the Ruby parser and accesses the HTML document.
using CGI.RB



Ruby can invoke the CGI library to write more complex CGI scripts.



The following code calls the CGI library to create a script CGI script.


#!/usr/bin/ruby
 
require ' cgi '
 
cgi = cgi.new
puts Cgi.header puts
"


In the following code, a CGI object is created and the header information is printed.
form Processing



There are two ways to get data from a form submission (or a parameter in a URL) using a CGI library, such as url:/cgi-bin/test.cgi? Firstname=zara&lastname=ali.



You can use cgi#[] to get the parameters FirstName and LastName directly:


#!/usr/bin/ruby
 
require ' cgi '
cgi = cgi.new
cgi[' FirstName '] # => ["Zara"]
cgi[' LastName '] # => [ "Ali"]


Another way to get the form data:


#!/usr/bin/ruby
 
require ' cgi '
cgi = cgi.new
h = cgi.params # => {"FirstName" =>["Zara"], "LastName" = >["Ali"]}
h[' FirstName '] # => ["Zara"]
h[' LastName ']  # => ["Ali"]


The following code retrieves all the key values:


#!/usr/bin/ruby
 
require ' cgi '
cgi = cgi.new
cgi.keys     # => ["FirstName", "LastName"]


If the form contains more than one field with the same name, the value of the same field is saved in the array.



The following example specifies three of the same field "name" in the form, the values are "Zara", "Huma" and "Nuha":


#!/usr/bin/ruby
 
require ' cgi '
cgi = cgi.new
cgi[' name ']    # => "Zara"
cgi.params[' name '] # => ["Zara", "Huma", "Nuha"]
Cgi.keys      # => ["name"]
cgi.params     # => {"name" =>["Zara", "Huma", "Nuha"]}


Note: Ruby automatically determines the get and POST methods, so there is no need to discriminate between the two methods.



The following are the relevant HML codes:


<html>
<body>
<form method="POST" action="http://www.example.com/test.cgi">
First Name :<input type="text" name="FirstName" value="" />
<br />
Last Name :<input type="text" name="LastName" value="" />
 
<input type="submit" value="Submit Data" />
</form>
</body>
</html>


 
 


Create form forms and HTML



CGI contains a number of ways to create HTML, and each HTML tag has a corresponding method. Before using these methods, you must create a CGI object by Cgi.new.



To make the tags more simple to nest, these methods take the content as a code block, and the code block returns the string as the content of the label. As shown below:


#!/usr/bin/ruby
 
require "cgi"
cgi = cgi.new ("Html4")
cgi.out{
  cgi.html{
   "\ n" + Cgi.title{"This is a Test"} +
   cgi.body{"\ n" +
     cgi.form{"\ n" +
      cgi.hr +
      cgi.h1 {"A form:"} + "\ n" +cgi.textarea ("Get_text") + "\ n" +
      cgi.br +
      cgi.submit
     }
}}


String escape



When you are dealing with parameters in the URL or HTML form data, you need to escape the specified special characters, such as: quotation marks ("), backslash (/).



The Ruby CGI object provides the Cgi.escape and Cgi.unescape methods to handle the escape of these special characters:


#!/usr/bin/ruby
 
require ' CGI '
puts Cgi.escape (Zara ali/a Sweet & Sour Girl ")


The above code execution results are as follows:


#!/usr/bin/ruby
 
require ' CGI '
puts Cgi.escape (Zara ali/a Sweet & Sour Girl ")


Another set of instances:


#!/usr/bin/ruby
 
require ' CGI '
puts cgi.escapehtml (' 


The above code execution results are as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.