search

Home  >  Q&A  >  body text

python - flask 启动后接受不到请求

flask 启动后接受不到请求(postman: Could not get any response
There was an error connecting to http://...:...

代码结构:

server/
       run.py
       server/
              __init__.py
              log.py
              views/
                    __init__.py
                    container.py

run.py:

from docker_server import app
if __name__ == '__main__':
    app.run(port=8082, debug=True)

__init__.py under server dir:

from flask import Flask
app = Flask(__name__)
from server.views import container
app.register_blueprint(container.mod)

container.py:

mod = Blueprint('container', __name__, url_prefix='/container')
@mod.route("/", methods=['GET'])
def container_test():
    logger.debug("container test")
    return None

stackoverflow上也提过:

http://stackoverflow.com/ques...

大家讲道理大家讲道理2892 days ago492

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 09:24:15

    The directory where you are creating mod的时候指定了url_prefix='/container',访问的时候这样看看http://localhost:8082/container/。而且使用蓝图的时候还要指定views, example of the official website (if specified, this blueprint may not be found):

    from flask import Blueprint, render_template, abort
    from jinja2 import TemplateNotFound
    
    simple_page = Blueprint('simple_page', __name__,
                            template_folder='templates')
    
    @simple_page.route('/', defaults={'page': 'index'})
    @simple_page.route('/<page>')
    def show(page):
        try:
            return render_template('pages/%s.html' % page)
        except TemplateNotFound:
            abort(404)

    In addition, the blueprint recommends using it like this:

    app/
        your_blueprint/
            __init__.py
            views.py
        __init__.py
        

    app/__init__/py:

    from flask import Flask
    def create_app():
        app = Flask(__name__)
        
        from .your_blueprint import your_blueprint as main_blueprint
        app.register_blueprint(main_blueprint)
        return app
        

    app/your_blueprint/__init__.py:

    from flask import Blueprint
    
    main  = Blueprint('main', __name__)
    
    from . import views

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:24:15

    Are you accessing it on the machine running flask? If not, it must be inaccessible according to your code. Just like the answer in stackoverflow, if you want to access from other machines, you need to configure a startup parameter, otherwise flask will only listen to requests on 127.0.0.1 by default.

    reply
    0
  • Cancelreply