0

I'm trying to create a python Flask REST web API. Since Flask development server is not suitable for production, I tried to use cherrypy application server.

Following is the Flask app I tried to expose via cherrypy

from flask import Flask,request
from flask_restful import Api,Resource, reqparse

app= Flask(__name__)
api = Api(app)

class Main (Resource):
    def get(self):
        return "Hello Flask"

if __name__ == '__main__':
    api.add_resource(Main, "/testapp/")
    app.run(debug=True)

Following is the cherrypy script I have created

try:
from cheroot.wsgi import Server as WSGIServer, PathInfoDispatcher
except ImportError:
    from cherrypy.wsgiserver import CherryPyWSGIServer as WSGIServer, WSGIPathInfoDispatcher as PathInfoDispatcher

from stack import app

d = PathInfoDispatcher({'/': app})
server = WSGIServer(('127.0.0.1', 8080), d)

if __name__ == '__main__':
   try:
      server.start()
      print("started")
   except KeyboardInterrupt:
      server.stop()

I have saved this script as "run.py" in my project directory. When I run this it doesn't show any error, which made me to thin this is correct.

But unfortunately I cant access this using the url

Theoretically, url for this API should be some thing like follow http://127.0.0.1:8080/testapp/

But it throws 404 with the message

"The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."

What am I doing wrong ?

1 Answer 1

1

The

api.add_resource(Main, "/testapp/")

in your file stack.py is not executed if the file is included from your run.py as the condition

if __name__ == '__main__':
...

is not true (in the context of stack.py).

Moving the call to api.add_resource(...) to a position outside the if-main-condition (so it is always executed) should solve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.