Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

Python web application with flask on local machine

I'm new to python and Flask, and I have a project from the production ENV which I'm trying to run on my local. I was able to install all the packages and bring them up on http://127.0.0.1:5000, but the problem is that is the only page that actually works on my local. and when I try to do Authorization or even simple post, it does not do anything on my local ( I put some print on the other files and none of them get fire) so I assume they keep going to production as it does have some APIs as well. Here is the main page (application.py) which is working on my local.

import os
import jwt
import logging
from datetime import datetime, timedelta

from http import HTTPStatus
from pydantic import BaseModel
from passlib.context import CryptContext
from flask import Flask, request, jsonify
from flask_restplus import Api, Resource, fields
from werkzeug.middleware.proxy_fix import ProxyFix
from applicationinsights.flask.ext import AppInsights

app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
api = Api(app, doc='/')
ns = api.namespace(name='Room Parsing', path='/')
swaggerTokenParser = api.parser()
swaggerTokenParser.add_argument('username', location='form')
swaggerTokenParser.add_argument('password', location='form')

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
rtp = RoomTitleParser(room_prototype_XX_path)

ALGORITHM = "HS256"


app.config["SECRET_KEY"] = os.getenv('SECRET_KEY')
app.config["APPINSIGHTS_INSTRUMENTATIONKEY"] = os.getenv('APPINSIGHTS_INSTRUMENTATIONKEY')

appinsights = AppInsights(app)
app.logger.setLevel(level=logging.INFO)
logger = app.logger



@ns.route("/api/room/parser")
class RoomParser(Resource):
    
    @api.expect(swaggerRoom)
    @api.doc(description='Process a JSON object with a room description and a unique identifier in order to run it through the parser. This will result in a list of keywords which will be extracted from the description. The result will be returned in a JSON format')
    def post(self):
        try:
            
            room_desc = "deluxe suite queen ocean view"
            room_id = "ID123"

                    
            print('11111111111')
    
                    

            if not room_desc or not room_id:
                    return make_json_error_message("Please send a a room with description and id",HTTPStatus.BAD_REQUEST)
    
                room_dict = dict(Room(description=room_desc, id=room_id))

                print(parsed)

                parsed = rtp.parse_title(room_dict)

                print(parsed)

                return jsonify(parsed['room'])
            except Exception as e:
                logger.error("Error parsing a room: " + repr(e))
                return make_json_error_message("We have encountered an error. Please try again later",HTTPStatus.BAD_REQUEST)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

As you can see I have some print statements and all of them are working on my local consol. But when I track down the code for Example this line,

parsed = rtp.parse_title(room_dict)

and put some print command inside the parse_title() function which is located in another file, I do NOT see any output in the console as well as webpage!

Why? I have no idea!!! LOL and that is why I'm here.

I believe it might be related to the @ns.route("/api/room/parser") that I have on top of the class, but not sure.

Can you guys please drop some knowledge here so I can learn and get this code to work on my local completely?

Thanks for your help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With what you've provided, there doesn't appear to be any reference to the production environment.

The only thing that sticks out to me is

app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)

The Werkzeug Documentation states that this middleware can set REMOTE_ADDR, HTTP_HOST from X-Forwarded headers. You might try removing that for a bit and see if that helps. There might be some reference to production in that proxy. I don't know enough about that middleware to know for sure however.

It might be helpful to know of any other configuration information or environment you have setup.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...