Detailed description of Python's CGIHTTPServer interaction implementation,

Source: Internet
Author: User

Detailed description of Python's CGIHTTPServer interaction implementation,
Introduction

For backend server developers, sometimes some of their services need to be directly exposed to PM or other RD users. In this case, a web service needs to be set up to allow simple interaction with front-end users, generally, Apache or Nginx is used as the backend of the webserver to process cgi, fcgi programs, or scripts. Of course, this approach is safe and formal.

But I personally prefer a simpler method: to use the CGIHTTPServer that comes with python as the server, then interact with each other through a simple html page, and directly call the master control script through the post request, interact with users.

Usage and Examples

Start the service

Python's CGIHTTPServer is generally installed with python. The following command can be used to start Python. To facilitate directory organization, we recommend that you create a directory, such as web, and then run the following command.

nohup python -m CGIHTTPServer 8088 &

The above command enables the CGIHTTPServer to run in the non-interrupted background. You can run the log to view it through nohup. out in the current directory.

Write interaction page

Create an index.html file in the current directory of the startup server. The content is as follows.

<! DOCTYPE html> 

The function of this interface is very simple. A form is used to receive the radius parameter submitted by the user, and return the result after calculating the perimeter. THE post request is used.

CGI script

This is what our backend developers are most interested in. This script is used to complete the main logical operations and return them, because our backend students focus more on the correctness and rigor of logic processing instead of the gorgeous interactive interface. Based on my experience, no matter how complicated the backend processing process is, in the end, you can use a script to package and get the output according to the input. The input here is the parameter submitted by the user, and the output is the content to be returned after processing.
We recommend that you store this script in the newly created cgi-bin directory under the server running directory.

#! /Bin/bashmysql_bin =/home/work/mysql/bin/mysqlecho "Content-Type: text/html; charset = gbk "echo" "echo" <br/> "radius = 0c_length = 0if [[" $ REQUEST_METHOD "=" POST "]; then read vars echo "$ vars" | awk-F "=" '{print $2}'> temp dos2unix temp radius = 'cat temp 'c_length = $ (echo "scale = 2; 2*3.14 * $ radius "| bc) echo" <br/> "echo" <table border = "5" cellpadding = "10"> "echo" Userid Info: "echo" <tr> "echo" <td> radius </td> <td> perimeter </td> "echo" </tr> "echo" <tr> "echo "<td>" $ radius "</td> <td>" $ c_length "</td>" echo "</tr>" echo "</table>" fi

Summary

The above is the main content of User Interaction Using Python's CGIHTTPServer. Its advantage is its ease of development and use, allowing backend students to focus more on business logic processing related content. The disadvantage may be that it is not formal enough and risky to use it. Because it is purely CGI protocol interaction, remember to check a bash cgi vulnerability before and it may be risky to use it, therefore, this method is more suitable for developing a set of easy-to-use tools, and it is not recommended to expose them to external users.

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.