linux arrangement python3 CGI

Article Updated: 2020-03-04

Note 1: Install the python See also: python's installation and basic grammar
Note 2: Configure a web environment See also: Windows & Linux using an integrated environment to build web server
Note 3: Windows Configuration CGI See: use python under windows environment CGI configuration
Note 4: More see Configuring CGI: Python CGI programming practice

First, configure the access to the CGI directory

1, install httpd

This is certainly not to say, CGI is providing web-based services, you definitely have to install a web service program.

Pick httpd, the terminal performs sudo yum install httpdthe installation.

2, find the cgi-bin directory and write a small program

Installed httpdlater in /var/www/the directory will be a cgi-bindirectory, this is the default cgiplacement program directory, can be used directly.

In this directory can be written directly to a CGI program, and then try to access.

For chestnut:

#! /usr/bin/env python
# -*- coding:UTF-8 -*-
print('Content-Type: text/html; charset=utf-8\n')

import os
print("你好")
print("<br>你的IP是:%s"%os.environ['REMOTE_ADDR'])
print ("<br>你的UA是:%s"%os.environ['HTTP_USER_AGENT'])
print("<br>你的请求端口是:%s"%os.environ['REMOTE_PORT'])
print("<br>你的请求路径是:http://anti-fraud.wiki%s"%os.environ['REQUEST_URI'])
print("<br>你的请求方法是:%s"%os.environ['REQUEST_METHOD'])

Explain:
1, the first line to declare the CGI program interpreter, here is the python, of course, also be used python3. However, when the output of Chinese python3 might be some problems, we discuss how to solve this later. Of course, you can also write path #! /usr/bin/python
2, the second line is to specify the encoding, note that after the second line of this code may be interpreted as a comment rather than coding.
3, the third line in response to print header information, a carriage return at the end is the end of the head.
4, several lines below the output of a number of environmental variables, such as the visitor's IP, UA and other information.

Access effect is as follows:
Figure I

Second, add / change the CGI program path

1, the path change CGI

Although the program is up and running. However, the cgi-binpath to change to change how I do, do not panic, look at the operation.

(1) edit /etc/httpd/conf/httpd.confthe configuration file.
Recommended vimeditor, command mode, press /the key and then type the cgisearch.
Locate the cgi
ScriptAlias cgi refers to the alias path, where we will change it ok, as shown below:
! [Change cgi alias] (https://img-blog.csdnimg.cn/20200304224650819.png
save and exit after the restart httpd configuration file to take effect and then try to access.

access

2, add CGI path

If you already have the path does not meet our needs, we do not panic, we own a plus.

step
Editing a configuration file step, edit the content as follows:
Here we add a web access path to hellothe actual file path is /var/www/hello/the path cgi program
Add Path

We try to visit at:
Successful visit

Third, resolve python3 CGI output of Chinese garbage problem

1, garbled phenomenon

Garbled program
Access Effect:
Garbled phenomenon
Logging:

AH01215: UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range(128)

2, the solution

1, plus the head of the program code is shown below:

#! /usr/bin/env python3
# -*- coding:UTF-8 -*-
import codecs, sys
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer)
print('Content-Type: text/html; charset=utf-8\n')

import os
print("hello")
print("你好")
print("<br>你的IP是:%s"%os.environ['REMOTE_ADDR'])
print ("<br>你的UA是:%s"%os.environ['HTTP_USER_AGENT'])
print("<br>你的请求端口是:%s"%os.environ['REMOTE_PORT'])
print("<br>你的请求路径是:http://anti-fraud.wiki%s"%os.environ['REQUEST_URI'])
print("<br>你的请求方法是:%s"%os.environ['REQUEST_METHOD'])
print("end")

In the first program header code above was added 3- 5, line, effect access as follows:
Successful visit
2, have been tested, the following header code equally effective

#! /usr/bin/env python3
# -*- coding:UTF-8 -*-
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.detach(),encoding='utf-8')
print('Content-Type: text/html; charset=utf-8\n')

四、Enjoy!

Published 75 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_21516633/article/details/104506047
Recommended