The original address based on Python cgihttpserver simple Interactive Implementation introduction
For server backend developers, there are times when you need to expose some of your services directly to PM or other Rd uses, and you need to build a Web service that can interact with the front-end user in a simple way, It is generally safe and formal to use either Apache or Nginx as a webserver back-end with CGI or fcgi programs or scripts.
But I personally prefer a simpler approach: using Python's own cgihttpserver as a server, interacting with a simple HTML page, calling a master script directly from a POST request and interacting with the user.
working with methods and examples Start a service
Python's cgihttpserver is typically installed with Python and can be started with the following commands, and for easy organization of the directory, it is recommended to create a directory, such as the Web, and then run the following command.
Nohup python-m cgihttpserver 8088 &
The above command allows Cgihttpserver to run in a non-disruptive background, and running log can be viewed through the nohup.out in the current directory. Writing Interactive pages
In the current directory of the startup server, create a index.html file, which is written as follows.
<! DOCTYPE html>
The function of this interface is very simple, the use of a form form to receive the user's submitted radius parameters, and then calculate the perimeter after the return, using the POST request. CGI Scripts
This is our backend development classmate most concerned about, this script is used to complete the main logical operation to return, because we back-end students focus more is not the interface of the gorgeous but logical processing of the correctness and rigor, according to my experience, the backend no matter how complex the processing process, In the end, you can use a script to package, according to input to get output, which is the input of our users to submit the parameters, the output is that we have to go through layers of processing after the need to return the content.
This script is recommended to be placed in the newly established Cgi-bin directory under the server run directory.
#!/bin/bash
mysql_bin=/home/work/mysql/bin/mysql
echo "content-type:text/html; CHARSET=GBK "
echo" "
echo" <br/> "
radius=0
c_length=0
if [[" $REQUEST _method "=" POST "]]; Then
read VARs
echo "$vars" | awk-f "=" ' {print $} ' > Temp
dos2unix temp
radius= ' cat temp '
c_le Ngth=$ (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
This is the main content of user interaction using Python's cgihttpserver, and its advantage is that it is easy to develop and use, so that the back-end students can focus more on the business logic processing related content. The disadvantage may be that it is not formal enough, the use of risk, because the use of pure CGI protocol interaction, remember to troubleshoot a bash before the CGI leak, can be used to risk, so this approach is more suitable for the development of an internal use of simple tools, not recommended to external users exposed.