提交处理请求时,为什么会出现403错误?

时间:2020-09-26 10:16:33

标签: python cgi

我在ubuntu server.py上启动python本地服务器:

from http.server import HTTPServer, CGIHTTPRequestHandler

server_address = ("", 8000)
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
httpd.serve_forever()

开始并打开index.html:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Обработка данных форм</title>
</head>
<body>
    <form action="/cgi-bin/form.py">
        <input type="text" name="TEXT_1">
        <input type="text" name="TEXT_2">
        <input type="submit">
    </form>
</body>
</html>

然后我发送GET请求,但我收到403代码错误:

Error response
Error code: 403
Message: CGI script is not executable ('/cgi-bin/form.py').
Error code explanation: HTTPStatus.FORBIDDEN - Request forbidden -- authorization will not help.

form.py:

#!/usr/bin/env python3
import cgi

form = cgi.FieldStorage()
text1 = form.getfirst("TEXT_1", "не задано")
text2 = form.getfirst("TEXT_2", "не задано")

print("Content-type: text/html\n")
print("""<!DOCTYPE HTML>
        <html>
        <head>
            <meta charset="utf-8">
            <title>Обработка данных форм</title>
        </head>
        <body>""")

print("<h1>Обработка данных форм!</h1>")
print("<p>TEXT_1: {}</p>".format(text1))
print("<p>TEXT_2: {}</p>".format(text2))

print("""</body>
        </html>""")

我尝试使用Win 10,并且效果很好。

我知道这样在Linyx上启动服务器:

python3 -m http.server --cgi

但是我需要从文件开始。

1 个答案:

答案 0 :(得分:1)

好吧,那我想起来很简单:

chmod +x form.py

现在可以了。

相关问题