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')

I can run the file fine with python api.py

but when i try to run it with waitress I get the following error

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.

My question is, why am i getting this error. I don't believe I am working out of the request context. It seems to work just how I want it to on the dev server but not on any other wsgi servers. Waitress is the only one that gave me this error.

Spyderz
  • 19
  • 1
  • 4

2 Answers2

1

refer to this topic from flask documentation

You need to tell Waitress about your application, but it doesn’t use FLASK_APP like flask run does. You need to tell it to import and call the application factory to get an application object.

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

that means waitress server failed to load your Flask app with this argument api:pac

once your server is up and running you can make call to that endpoint like :

http://localhost:8080/api/poc

finally i would recommend you to rely on the best practices when developing Flask apps

have a look at this topic on how to setup a Flask app with app factory pattern and as you may know python 2 is literally dead and not supported as of jan 2020, use python 3 and update all dependencies.

cizario
  • 3,995
  • 3
  • 13
  • 27
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

I had a similar problem try something like this. When you run the application by waitress you just call a function named "create_app" which contains all your flask app. Now type waitress-serve --call "<application_name>:create_app"