Internal Server Error问题

刚学习前端,遇到了Internal Server Error的问题
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

问题具体描述:
把python代码中do_res函数的1和2部分注释掉只执行return内容可以正常进行,但取消1和2的注释后就会出现开头所出现的情况

python代码如下:

from flask import Flask, render_template, request

app = Flask(__name__)


@app.route('/requests')
def req():
    return render_template('requests.html')



@app.route('/do/req')
def do_req():
    # 1.接收用户提交的数据
    username = request.args.get('user')
    password = request.args.get('pwd')
    print(username, password)

    # 2.将用户名和密码写入文件/数据库
    with open('account.txt', mode='a', encoding='udf-8') as f:
        line = "{}|{}\n".format(username, password)
        f.write(line)

    # 3.返回信息
    return "注册成功"


if __name__ == '__main__':
    app.run()


requests.html文件如下:


```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="get" action="/do/req">
    <div>
        用户名:<input type="text" name="user">
    </div>
    <div>
        密码:<input type="password" name="pwd">
    </div>
    <input type="submit" value="注 册">
</form>
</body>
</html>


报错意思是:服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序中存在错误。

1.with open('account.txt', mode='a', encoding='udf-8') as f: 中的 udf-8 应该为 utf-8,否则会导致编码错误。

2.username = request.args.get('user') 和 password = request.args.get('pwd') 应该加上默认值,如 request.args.get('user', ''),以免没有传入参数而导致代码出错。

感觉是你写文件出错,比如说没有权限,具体你调试下吧,或者加上 try catch 输出下错误信息看看