When I start the server, the console shows
python2 web.py
* Running on http://0.0.0.0:8005/ (Press CTRL+C to quit)
* Restarting with inotify reloader
And if I tried to make a request to the server, the flask server will be down.
I have no ideas what's wrong on it, it didn't throws any error log.
Here are what I imported
# -*- coding: utf8 -*-
from flask import request, url_for
from flask import json
from flask import Response
from flask import Flask, request, jsonify
from flask_request_params import bind_request_params
import datetime
import pandas as pd
import pymongo
import pdb
from webargs import Arg
from webargs.flaskparser import use_args, use_kwargs
import urlparse
from mongo import Mongo
import yaml
import time, functools
from functools import wraps
from pdb import set_trace
from flask import g
from pandas_helper import PandasHelper
inotify seems to be coming from Werkzeug, but I can't find anything suggesting that it should be using 0.0.0.0- this only appears with the inbuilt development server run function.
For development work werkzeug suggests:
from werkzeug.wrappers import Request, Response
#Request.application
def application(request):
return Response('Hello World!')
if __name__ == '__main__':
from werkzeug.serving import run_simple
run_simple('localhost', 4000, application)
Related
SCENARIO
I am using a python flask server to serve up data from my remote database. I am tempting to use the requests framework to connect to my database with a held connection.
TECHNOLOGY USED
Python 3.10
Flask 2.0.3
Poetry 1.0 (for dependencies)
Requests 2.28.1 (added with Poetry)
IBM Code Engine (cloud platform/environment)
ISSUE
My project works just fine up until I add the line import request at the top of my server.py. When I add this line no responses are given by my server and it times out.
CODE
The below outputs { "hello": "there" }, because import requests was not added.
import os
import sys
import json
from collections import Counter
from flask import Flask, render_template, request, session, url_for, jsonify
app = Flask(__name__)
#app.route('/', methods=['POST', 'GET'])
def main():
return { 'hello': "there" }
if __name__ == 'main':
app.run()
The below stalls and servers no response, because import requests was added.
import os
import sys
import json
import requests # adding this
from collections import Counter
from flask import Flask, render_template, request, session, url_for, jsonify
app = Flask(__name__)
#app.route('/', methods=['POST', 'GET'])
def main():
return { 'hello': "there" }
if __name__ == 'main':
app.run()
QUESTION
Why can't I import requests, and if I'm missing something could you please leave a code answer as to what I need to do to get this working?
(I've noticed some other frameworks can't be imported either)
I am trying to add some monitoring to a simple REST web service with flask and mongoengine and have come across what I think is a lack of understanding on my part of how imports and mongoengine is working in flask applications.
I'm following pymongo's documentation on monitoring : https://pymongo.readthedocs.io/en/3.7.2/api/pymongo/monitoring.html
I defined the following CommandListener in a separate file:
import logging
from pymongo import monitoring
log = logging.getLogger('my_logger')
class CommandLogger(monitoring.CommandListener):
def started(self, event):
log.debug("Command {0.command_name} with request id "
"{0.request_id} started on server "
"{0.connection_id}".format(event))
monitoring.register(CommandLogger())
I made an application_builder.py file to create my flask App, code looks something like this:
from flask_restful import Api
from flask import Flask
from command_logger import CommandLogger # <----
from db import initialize_db
from routes import initialize_routes
def create_app():
app = Flask(__name__)
api = Api(app)
initialize_db(app)
initialize_routes(api)
return app
The monitoring only seems to works if I import : CommandLogger in application_builder.py. I'd like to understand what is going on here, how does the import affect the monitoring registration?
Also I'd like to extract monitoring.register(CommandLogger()) as a function and call it at a latter stage in my code something like def register(): monitoring.register(CommandLogger())
But this doesn't seem to work, "registration' only works when it is in the same file as the CommandLogger class...
From the MongoEngine's doc, it seems important that the listener gets registered before connecting mongoengine
To use pymongo.monitoring with MongoEngine, you need to make sure that
you are registering the listeners before establishing the database
connection (i.e calling connect)
This worked for me. I'm just initializing/registering it the same way as I did other modules to avoid circular imports.
# admin/logger.py
import logging
from pymongo import monitoring
log = logging.getLogger()
log.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
class CommandLogger(monitoring.CommandListener):
# def methods...
class ServerLogger(monitoring.ServerListener):
# def methods
class HeartbeatLogger(monitoring.ServerHeartbeatListener):
# def methods
def initialize_logger():
monitoring.register(CommandLogger())
monitoring.register(ServerLogger())
monitoring.register(HeartbeatLogger())
monitoring.register(TopologyLogger())
# /app.py
from flask import Flask
from admin.toolbar import initialize_debugtoolbar
from admin.admin import initialize_admin
from admin.views import initialize_views
from admin.logger import initialize_logger
from database.db import initialize_db
from flask_restful import Api
from resources.errors import errors
app = Flask(__name__)
# imports requiring app
from resources.routes import initialize_routes
api = Api(app, errors=errors)
# Logger before db
initialize_logger()
# Database and Routes
initialize_db(app)
initialize_routes(api)
# Admin and Development
initialize_admin(app)
initialize_views()
initialize_debugtoolbar(app)
# /run.py
from app import app
app.run(debug=True)
then in any module...
from admin.logger import log
from db.models import User
# inside some class/view/queryset or however your objects are written...
log.info('Saving an item through MongoEngine...')
User(name='Foo').save()
What I'm trying to figure out now is how to integrate Flask DebuggerToolbar's Logging panel with the monitoring messages from these listeners...
Heyo,
I am trying to create websocket server using flask-socketio but I ran into a problem, the sockets drop! I am running gunicorn with 4 workers and I am aware of the fact that socketio library is storing the data in-memory(that is why I have redis installed because I have encountered similar issue before), I have pointed an app to the redis server but it still doesn't work with 4 workers, however running gunicorn with 1 worker is okay.
Here is my application init code:
from flask import Flask
from flask_json import FlaskJSON, JsonError, json_response, as_json
from webroutes import shallow
from userapi import user_api
from app_service import shallow_service
import os
from flask_socketio import SocketIO
from reddissession import RedisSessionInterface
from socket_worker import Messenger
import eventlet
eventlet.monkey_patch()
# from gevent.wsgi import WSGIServer
application = Flask(__name__)
application.secret_key = os.urandom(64)
FlaskJSON(application)
socketio = SocketIO()
socketio.on_namespace(Messenger("/messenger_socket"));
application.register_blueprint(shallow)
application.register_blueprint(user_api)
application.register_blueprint(shallow_service)
application.config['TEMPLATES_AUTO_RELOAD'] = True
application.session_interface = RedisSessionInterface()
socketio.init_app(application, message_queue='redis://')
Here is what I get in console.log:
Output
Please send help, thanks
Heyo, I solved it!
It is on reddit though:
https://www.reddit.com/r/flask/comments/98xhw2/flasksocketio_through_gunicorn_and_redis_as_a/
All
I have a flask_rest plus api over flask app in python. I want to deploy the same using the uwsgi format on nginx server. I searched for quite a long but didn't found the write solution.Any leads would be helpful..
Thanks in advance.
Below is the code snippet
import datetime
from fileinput import filename
import json
import os
from flask import request
from flask_restplus import Resource, Api
from werkzeug.utils import secure_filename
from APP.Application import app
from ApiConfig.ObjectConfig import Trainer, cData, Tmodal, Amodal, Cmodal
from ApiConfig.SA_utility import sentiment_sorting
from ApiConfig.appFileConfig import UPLOAD_FOLDER
from Classification.Classifier import classify
from DataBase.DB import db
from ExcelParser_DataFrame.Excel_Parser import ExcelDataFrame
from Vectorizer.Data_vector import vectorizer
from Mapper.UI_Map import UI_Map
from werkzeug.contrib.fixers import ProxyFix
api = Api(app)
app.wsgi_app = ProxyFix(app.wsgi_app)
#api.route('/sentiment-analysis/trainer')
class Upload_File(Resource):
def post(self):
..............
if __name__ == '__main__':
db.init_app(app)
db.create_all()
app.run(host='0.0.0.0')
My wsgi.py file looks like this
import sys
sys.path.insert(0, "/home/gulshan/Downloads/SA_PANDAS_SGDC/src")
from APP.Application import app
application = app
i tried using this wsgi file and started the server by firing the below command on my linux machine
uwsgi --http :5000 --wsgi-file wsgi.py --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191
when i tried accessing the url using Postman then i get error as shown below:-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
My APP.Application file looks like this:-
from flask import Flask
from ApiConfig import Settings as settings
app = Flask(__name__)
#app.config['SERVER_NAME'] = settings.FLASK_SERVER_NAME
app.config['SQLALCHEMY_DATABASE_URI'] = settings.SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = settings.SQLALCHEMY_TRACK_MODIFICATIONS
app.config['SWAGGER_UI_DOC_EXPANSION'] = settings.RESTPLUS_SWAGGER_UI_DOC_EXPANSION
app.config['RESTPLUS_VALIDATE'] = settings.RESTPLUS_VALIDATE
app.config['RESTPLUS_MASK_SWAGGER'] = settings.RESTPLUS_MASK_SWAGGER
app.config['ERROR_404_HELP'] = settings.RESTPLUS_ERROR_404_HELP
app.config['SQLALCHEMY_POOL_SIZE'] = settings.SQLALCHEMY_POOL_SIZE
#app.config['SQLALCHEMY_POOL_TIMEOUT'] = settings.SQLALCHEMY_POOL_TIMEOUT
app.config['SQLALCHEMY_POOL_RECYCLE'] = settings.SQLALCHEMY_POOL_RECYCLE
app.config['SQLALCHEMY_MAX_OVERFLOW'] = settings.SQLALCHEMY_MAX_OVERFLOW
Got a solution working for me although it seems to be weird way of doing this:-
Changed my uwsgi.py
import sys
sys.path.insert(0, "/home/gulshan/Downloads/SA_PANDAS_SGDC/src")
from APP.Application import app
from flask import Flask
from SA_APP.SA import appnew
application = app
application = appnew
and added this line in my first code snippet file containing main call
api = Api(app)
appnew = app.
I am newbiee on pyhton development and trying to make the following example work.
The following code is for service authentication. Whenever I call localhost:3000/callback through browser, I am receiving code error because it is null.
When I create a webpage on auth0, then it puts all required information and then make the source code available to use. However, code is not included. I wonder what needs to inserted.
token = get_token.authorization_code(AUTH0_CLIENT_ID,
AUTH0_CLIENT_SECRET, code,
AUTH0_CALLBACK_URL)
auth0.v3.exceptions.Auth0Error: invalid_request: Missing required
parameter: code
.env
AUTH0_CLIENT_ID=xxxxxxxxxxx
AUTH0_DOMAIN=xxxxxx.auth0.com
AUTH0_CLIENT_SECRET=v-xxxxx2yVHntpn01RoEMMxhj6RLxxxxxxxxxx
AUTH0_CALLBACK_URL=http://localhost:3000/callback
API_IDENTIFIER={API_AUDIENCE}
server.py
"""Python Flask WebApp Auth0 integration example"""
from functools import wraps
from urllib.parse import urlparse
from os import environ as env, path
import json
from auth0.v3.authentication import GetToken
from auth0.v3.authentication import Users
from dotenv import load_dotenv
from flask import Flask
from flask import redirect
from flask import render_template
from flask import request
from flask import send_from_directory
from flask import session
import constants
load_dotenv(path.join(path.dirname(__file__), ".env"))
AUTH0_CALLBACK_URL = env[constants.AUTH0_CALLBACK_URL]
AUTH0_CLIENT_ID = env[constants.AUTH0_CLIENT_ID]
AUTH0_CLIENT_SECRET = env[constants.AUTH0_CLIENT_SECRET]
AUTH0_DOMAIN = env[constants.AUTH0_DOMAIN]
#APP.route('/callback')
def callback_handling():
code = request.args.get(constants.CODE_KEY)
get_token = GetToken(AUTH0_DOMAIN)
auth0_users = Users(AUTH0_DOMAIN)
#Receive exception
token = get_token.authorization_code(AUTH0_CLIENT_ID,
AUTH0_CLIENT_SECRET, code, AUTH0_CALLBACK_URL)
user_info = auth0_users.userinfo(token['access_token'])
session[constants.PROFILE_KEY] = json.loads(user_info)
return redirect('/dashboard')
if __name__ == "__main__":
APP.run(host='0.0.0.0', port=env.get('PORT', 3000))