2

前言

大家好,当我尝试将flask模块用于python时,我遇到了一个非常奇怪的错误......首先让我解释一下我首先开始编写程序然后它停止工作没问题我的最后一个副本应该工作......对? 在我尝试恢复到当前版本代码之前的 2 个版本之后,我仍然遇到同样的错误

好的,所以发生的事情是当我尝试运行我的烧瓶应用程序时

      Traceback (most recent call last):
    File "/Users/me/Desktop/ASL server/router.py", line 47, in <module>
      from flask import *
    File "/Library/Python/2.7/site-packages/flask/__init__.py", line 17, in <module>
      from werkzeug.exceptions import abort
    File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/__init__.py", line 154, in <module>
      __import__('werkzeug.exceptions')
    File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/exceptions.py", line 71, in <module>
      from werkzeug.wrappers import Response
    File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/wrappers.py", line 26, in <module>
      from werkzeug.http import HTTP_STATUS_CODES, \
    File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/http.py", line 28, in <module>
      from urllib.request import parse_http_list as _parse_list_header
  ImportError: No module named request

目前我已尝试卸载该模块并将其与所有支持模块一起重新安装。该代码是错误发生之前的还原代码,现在错误再次发生(未对旧代码进行任何更改)我不知道问题是什么......

我的python代码是

    from flask import *
    import flask
    from flask import render_template
    import os
    import random
    import sys
    app = Flask(__name__)
    app.secret_key ="REST SECRET"
    ##############################################
    @app.errorhandler(503)                       #
    def page_not_found(e):                       #
        flash('503 Service Unavailable')         #
        return render_template('500.html'), 500  #
    @app.errorhandler(408)                       #
    def page_not_found(e):                       #
        flash('408 Request Timeout')             #
        return render_template('403.html'), 408  #
    @app.errorhandler(401)                       #
    def page_not_found(e):                       #
        flash('401- Restricted')                 #
        return render_template('403.html'), 401  #

    ##############################################
    def getip():
        c = request.remote_addr
        i = c.encode(encoding='UTF-8',errors='strict')
        ip = i.encode(encoding='UTF-8',errors='strict')

    @app.route('/')
    def home():
        ipa = getip()
        session[ipa] = ipa
        print session
        print ipa
        return render_template('index.html')


    @app.route('/donate')
    def donate():
        return render_template('donate.html')
    @app.route('/learn/fingerspell/end')
    def learnfinger():
        return render_template('Welcometofinger.html')
    @app.route('/learn/fingerspell',methods=["GET","POST"])
    def learnfingera():
        x = "a"
        if request.method == "POST":
            if request.form['next'] == "clear":
                return redirect('/learn/fingerspell/b')

        return render_template("fingerspell.html",error=x)

    @app.route('/learn/fingerspell/b',methods=["GET","POST"])
    def learnfingerb():
        x = "b"
        if request.method == "POST":
            if request.form['next'] == "clear":
                return redirect('/learn/fingerspell/c')

        return render_template("fingerspell.html",error=x)

    # And so on and on till you get to Z

    @app.route('/learn/fingerspell/z',methods=["GET","POST"])
    def learnfingerz():
        x = "z"
        if request.method == "POST":
            if request.form['next'] == "clear":
                return redirect('/learn/fingerspell/end')

        return render_template("fingerspell.html",error=x)



    @app.route('/learn/basic-words')
    def learnwordb():

        return render_template('bwordsmain.html')

    @app.route('/learn/basic-words/course')
    def learnwordb_course():
        flash("Welcome to your first lesson ")
        return 
    @app.route('/learn/basic-words/course/test/id1',methods=["GET","POST"])
    def test1baiscstart():
        score = 0
        if request.method == "POST":
            if request.form['a'] == "ASL":
                score += 1
        return render_template('test1.html')

    @app.route('/learn')
    def learn():
        return render_template('newopt.html')

    @app.route('/test/id/1',methods=["POST","GET"])
    def testabcs():
        score = 100
        missq = 0
        truea = 5
        if request.method == "POST":
            ab = request.form['qa']
            ba = request.form['qb']
            ca = request.form['qc']
            da = request.form['qd']
            ea = request.form['qe']
            a = str(ab).lower()
            b = str(ba).lower()
            c = str(ca).lower()
            d = str(da).lower()
            e = str(ea).lower()
            if True:
                if a != "asl is fun":
                    score -= 20
                    missq += 1
                elif b != "this isnt hard":
                    score -= 20
                    missq += 1
                elif c != "its fun":
                    score -= 20
                    missq += 1
                elif d != "m":
                    score -= 20
                    missq += 1
                elif e != "t":
                    score -= 20
                    missq += 1
                else:
                    flash("DEATH ERROR")
            flash("you scored "+ str(score)+"%  and missed " + str(missq) + " out of " + str(truea))
            return render_template('info.html')

        return render_template("abcstest.html")
    @app.route('/games')
    def games():
        return render_template("games.html")


    if __name__ == '__main__':
        port = int(os.environ.get('PORT', 5001))
        app.debug = True
        app.run(host='0.0.0.0',port=port)
4

2 回答 2

1

实际上,这两条语句的作用几乎相同:

from flask import *
import flask

当您尝试在烧瓶中使用方法时,您有 2 个选择:第一个:

from flask import *
c = request.remote_addr

第二:

import flask
c = flask.request.remote_addr

你注意到有什么不同吗?使用时from a_module import *,你可以使用模块中的方法名。使用import a_module时,你必须编译器你使用哪个模块,例如:a_module.method_name

于 2013-10-31T08:23:00.780 回答
1

有时目录会中断...尝试将文件从目录中复制出来并创建一个新目录,然后重新运行应该可以工作的服务器。

我想通了,因为它之前有效,然后又有效。你的代码很好!

于 2013-11-01T20:56:16.240 回答