gpt4 book ai didi

Python CGI 内部错误 login.html login.cgi

转载 作者:行者123 更新时间:2023-12-04 18:49:12 29 4
gpt4 key购买 nike

我目前正在尝试为我在带有 Ubuntu 的虚拟机上运行的实际本地网页创建一个简单的登录页面。
我创建了 LoginPage.html在位置 /var/www/html .
然后 HTML 文件调用 login.cgi /usr/lib/cgi-bin/login.cgi 中的文件.
我得到一个 Internal Server Error .日志基本上只显示了这一点:

"POST /cgi-bin/login.cgi HTTP/1.1" 500 799"http://localhost/LoginPage.html" "Mozialla/5.0 (X11; Ubtuntu; Linuxx86_64; rv:84.0) Geck/201000101 Firefox/84.0


HTML 文件似乎按预期工作,但是当我按下登录并重定向到 CGI 文件时,我在 CGI 文件上收到错误消息。我试图删除 CGI 文件中的所有内容,只留下几行,但仍然出现错误。
我在 cgi-bin 中的其他项目文件文件夹仍然可以正常工作。
<HTML>
<HEAD><TITLE>Login Page</TITLE></HEAD>
<BODY>
<CENTER>
<FORM method="POST" action="/cgi-bin/login.cgi">
<paragraph> Enter your login name: <input type="text" name="login">
<paragraph> Enter your password: <input type=password name="password">
<paragraph> <input type="submit" value="Connect">
</FORM>
</CENTER>
<HR>

</form>
</BODY>
</HTML>
#!/usr/bin/python3
import sys
import cgi
import os
import cgitb

sys.path.insert(0,'/usr/lib/project_name')
def header():
#print "Content-type: text/html\n"
print("<HEAD>")
print("<TITLE> title </TITLE>")
print("</HEAD>")

def Log():
print("<!DOCTYPE html>")
print("<HTML>")
print("<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">")
print(" <meta charset=\"utf-8\" />")
header()
print("BODY")
form = cgi.FieldStorage()
login = "login"
password = "test123"
if not (form):
header("Login Response")
print("<BODY>")
elsif (form.has_key("login") and form["login"].value == login and form.has_key("password") and form["password"].value == password):
header("Connected ...")
print("<BODY>")
print("<center><hr><H3>Welcome back,\" , form[\"login\"].value, \".</H3><hr></center>")
print("r\"\"\"<form><input type=\"hidden\" name=\"session\" value=\"%s\"></form>\"\"\" % (form[\"login\"].value)")
print("<H3><a href=\"/cgi-bin/projects.cgi\">Click here to start browsing</a></H3>")
else:
header("No success!")
print("<BODY>")
print("<H3>Please go back and enter a valid login.</H3>")

def footer():
print("</BODY>")
print("</HTML>")

print("Content-type:text/html\r\n\r\n")
cgitb.enable()
Log()
footer()
编辑:
这里是 error.log的内容解决 Internal Server Error 后:

[Tue Feb 02 08:40:41.199152 2021] [cgi:error] [pid 10292:tid140490049578752] [client 127.0.0.1:38888] AH01215: (2)No such file ordirectory: exec of '/usr/lib/cgi-bin/login.cgi' failed:/usr/lib/cgi-bin/login.cgi, referer: http://localhost/LoginPage.html[Tue Feb 02 08:40:41.199411 2021] [cgi:error] [pid 10292:tid140490049578752] [client 127.0.0.1:38888] End of script output beforeheaders: login.cgi, referer: http://localhost/LoginPage.html

最佳答案

更正设置:

No such file or directory: exec of '/usr/lib/cgi-bin/login.cgi' failed: /usr/lib/cgi-bin/login.cgi, referer: http://localhost/LoginPage.html


确保 CGI 脚本及其父目录具有正确的权限:
chmod 755 /usr/lib/cgi-bin/ /usr/lib/cgi-bin/login.cgi
此外,CGI 脚本似乎可能有 windows 行尾,因此请确保您也删除了这些行尾,例如通过运行 dos2unix login.cgi (参见 this post 了解更多详情)。
解决内部服务器错误:
首先,至少进行以下更改以更正您的语法(这是导致 Internal Server Error 的原因):
  • elsif应该是 elif
  • 您的 header函数应该接受一个参数,即 def header(title)
  • 你在哪里 form.has_key , 改为使用 in自从 has_key现在已弃用,例如"password" in form而不是 form.has_key("password")

  • 更正后的情况如下所示:
    elif "login" in form and form["login"].value == login and "password" in form and form["password"].value == password:
    顺便说一句,坚持 HTML5 ,所有最新的浏览器都支持, center tag现在已弃用(它已成为 marquee tag 的方式)。 Use CSS instead .
    此外,作为一个完整的旁注,这些天来,看到 并不常见。全部大写 用于 HTML 标签。你在某些地方使用过这种风格,但我 suggest dropping it in favor of lowercase tag names .
    简化 HTML 生成:
    除了以上,我推荐使用 f-strings用于字符串格式化以及 multi-line strings稍微简化一下逻辑。
    下面是一些如何增强代码的示例。
    您的标题函数可能看起来像这样使用 f-strings 和多行字符串,如建议的那样。 title参数是可选的。
    def header(title=""):
    print(f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title> {title} </title>
    </head>
    <body>
    """)
    您的页脚功能可能如下所示:
    def footer():
    print("""
    </body>
    </html>
    """)
    最后,您的 HTML 表单可能如下所示,使用 text-align: center; 对于居中:
    print(f"""
    <hr>
    <h3 style="text-align: center;">Welcome back { form["login"].value }</h3>
    <hr>
    <form>
    <input type="hidden" name="session" value="{ form["login"].value }">
    </form>
    <h3>
    <a href="/cgi-bin/projects.cgi">Click here to start browsing</a>
    </h3>
    """)
    美化 HTML:
    要进一步美化 HTML,您可以 import textwrap 然后使用 textwrap.dedent() 从 HTML 中删除常见的前导空格(由于 Python 中的缩进),例如
    print(textwrap.dedent(f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title> {title} </title>
    </head>
    <body>
    """))
    这有两个好处:
  • 它在通过网络发送之前删除了不必要的前导空格——因此您发送的数据更少,节省带宽
  • 当您使用客户端浏览器检查 HTML 源代码时,它会更漂亮——对调试很有用

  • 或者,使用 HTML 模板:
    进一步的增强是利用 HTML 模板框架,例如 Jinja2 .这将允许您将 HTML 存储到文件中,然后通过传递变量来呈现 HTML 文件。
    然后,您的 Python 代码会变得简单得多。您只需渲染模板并传递您想要的变量。例如,对于您的标题,它可能是这样的:
    template = env.get_template('header.html')
    print(template.render(title='Connected ...'))
    您需要设置 Environment首先像这样:
    from jinja2 import Environment

    env = Environment(
    loader=PackageLoader('package', 'templates')
    autoescape=select_autoescape(['html'])
    )
    并将您的 header.html名为 templates 的目录中的文件.
    您可能还想了解 seperation of concerns原则和 MVC architecture .使用这样的模板只是接近实现 MVC 的一步。
    关于安全性的最后一点:
    看起来您正在使用 HTML hidden字段来存储用户名,并将其视为 session token 。这是 highly insecure and won't scale .不过,它可能足以满足您的目的。改进它的一个简单方法是使用 Set-cookie header 将其存储为 cookie。 , 并将其设为 HttpOnly ,例如
    Set-cookie: session=value; HttpOnly
    在哪里 value可能是一些独特的 token (例如 uuid )。
    这比您当前的方法要好得多。
    但是,更好的是,您可以使用像 PyJWT 这样的库来实现安全性。反而。

    关于Python CGI 内部错误 login.html login.cgi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65991370/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com