key:期望的字节或bytearray,但得到了' int'

时间:2017-12-26 12:23:01

标签: python-3.x flask

from flask import Flask, render_template,request,url_for,make_response,redirect,session
from bs4 import BeautifulSoup
import sqlite3
import random
import models
import os
import socket


app = Flask(__name__)
app.config['SECRET_KEY'] = b"HelloWorld"

def check_session():
    try:
        loggedin = session['loggedin']
    except:
        session['loggedin'] = False
        loggedin = session['loggedin']

    return loggedin

@app.route('/')
def index():
    loggedin = check_session()
    #loggedin = True

    blogpost = models.Blogpost()

    posts = blogpost.conn.execute("SELECT path FROM posts ORDER BY timestamp DESC")

    post_content = []

    for post in posts:
        post_text = BeautifulSoup(open(post[0],'r').read(),'html.parser')
        post_content.append(post_text)

    return render_template('home.html',loggedin=loggedin,post_content=post_content)
    #return "HelloWorld"

@app.route('/login',methods = ['GET','POST'])
def login():

    if request.method == 'GET':
        return render_template('login.html',strike=0)   

    elif request.method == 'POST':

        username = request.form['username']
        password = request.form['password']
        user = models.User(username)

        result = user.authenticate(username,password)
        if result == False:
            session['loggedin'] = True
            session['username'] = username
            return render_template(home.html, session['loggedin']

if socket.gethostname() == "DESKTOP-D18" :
    if __name__ == '__main__':
        app.secret_key=1
        app.run(host=os.getenv('IP', '127.0.0.1'),port=int(os.getenv('PORT', 8080)),debug=True)
else :
    if __name__ == '__main__':
        app.secret_key=1

        app.run(host=os.getenv('IP', '0.0.0.0'),port=int(os.getenv('PORT', 8080)),debug=True)

这是我的Flask应用程序。当我运行它时,它会给出以下错误: key: expected bytes or bytearray, but got 'int'

1 个答案:

答案 0 :(得分:0)

使用行号可以更容易回答。

我的猜测可能是你试图将int转换为int:

port=int(os.getenv('PORT', 8080))

8080已经是一个int。如果你这样做,它是否有效:

port=int(os.getenv('PORT', '8080'))