0
import flask
from flask import request
from pacfiletesterAPI import PacFileTester
from urlparse import urlparse
from flask import jsonify

app = flask.Flask(__name__)
#app.config["DEBUG"] = False

@app.route('/pac')
def pac():
    r = request.args.get('p')
    url = request.args.get('u')
    domain = request.args.get('d')
    Parsed_url = urlparse(url)
    Parsed_domain = urlparse(domain)
    if r == '':
        return "'not enough arguments, please provide a vaild url"
    elif url == '':
        if domain == '':
            return "'not enough arguments, please provide a vaild url"
    elif domain == '':
        if url == '':
            return "'not enough arguments, please provide a vaild domain"
        if Parsed_url.netloc == '':
            domain = Parsed_url.path.rstrip('/')
        else:
            domain = Parsed_url.netloc
    if not Parsed_url.scheme != 'http' or Parsed_url.scheme != 'https':
        url = 'https://' + domain

    result = PacFileTester(r, url, domain)
    return jsonify(result)

#app.run(host= '0.0.0.0')

if __name__ == "__main__":
    app.run(host= '0.0.0.0')

我可以用 python api.py 很好地运行文件

但是当我尝试与女服务员一起运行它时,我收到以下错误

waitress-serve --call 'api:pac'
Traceback (most recent call last):
  File "/usr/bin/waitress-serve", line 11, in <module>
    sys.exit(run())
  File "/usr/lib/python2.7/site-packages/waitress/runner.py", line 280, in run
    app = app()
  File "/var/www/html/cloud_test3/api.py", line 13, in pac
    r = request.args.get('p')
  File "/usr/lib/python2.7/site-packages/werkzeug/local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/usr/lib/python2.7/site-packages/werkzeug/local.py", line 306, in _get_current_object
    return self.__local()
  File "/usr/lib64/python2.7/site-packages/flask/globals.py", line 38, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

我的问题是,为什么我会收到这个错误。我不相信我在请求上下文之外工作。它似乎就像我想要的那样在开发服务器上工作,而不是在任何其他 wsgi 服务器上。女服务员是唯一给我这个错误的人。

4

2 回答 2

1

从文档中参考这个主题flask

您需要告诉 Waitress您的应用程序,但它不像 flask run 那样使用 FLASK_APP。您需要告诉它导入并调用应用程序工厂以获取应用程序对象

$ waitress-serve --call 'myflaskapp:create_app'

这意味着waitress服务器无法Flask使用此参数加载您的应用程序api:pac

一旦您的服务器启动并运行,您就可以调用该端点,例如:

http://localhost:8080/api/poc

Flask最后,我建议您在开发应用程序时依赖最佳实践

看看这个关于如何Flask使用模式设置应用程序的主题app factory,你可能知道python 2它实际上已经死并且不支持jan 2020使用python 3和更新所有依赖项。

于 2020-06-25T16:12:09.983 回答
0
import flask
from flask import request
from pacfiletesterAPI import PacFileTester
from urlparse import urlparse
from flask import jsonify

def create_app():
    app = flask.Flask(__name__)
    
    @app.route('/pac')
    def pac():
        r = request.args.get('p')
        url = request.args.get('u')
        domain = request.args.get('d')
        Parsed_url = urlparse(url)
        Parsed_domain = urlparse(domain)
        if r == '':
            return "'not enough arguments, please provide a vaild url"
        elif url == '':
            if domain == '':
                return "'not enough arguments, please provide a vaild url"
        elif domain == '':
            if url == '':
                return "'not enough arguments, please provide a vaild domain"
            if Parsed_url.netloc == '':
                domain = Parsed_url.path.rstrip('/')
            else:
                domain = Parsed_url.netloc
        if not Parsed_url.scheme != 'http' or Parsed_url.scheme != 'https':
            url = 'https://' + domain
    
        result = PacFileTester(r, url, domain)
        return jsonify(result)
    return app

我有一个类似的问题尝试这样的事情。当您由服务员运行应用程序时,您只需调用一个名为“create_app”的函数,其中包含您所有的烧瓶应用程序。现在输入 waitress-serve --call "<application_name>:create_app"

于 2021-03-09T13:24:36.820 回答