Error 111 after following Werkzeug tutorial "shortly" - python

I followed the tutorial for Werkzeug "Shortly" here
And I get this error message after submitting a valid url.
Traceback (most recent call last)
File "/home/sadik/NLM/shortly/shortly.py", line 87, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/wsgi.py", line 579, in __call__
return self.app(environ, start_response)
File "/home/sadik/NLM/shortly/shortly.py", line 83, in wsgi_app
response = self.dispatch_request(request)
File "/home/sadik/NLM/shortly/shortly.py", line 33, in dispatch_request
return getattr(self, 'on_' + endpoint)(request, **values)
File "/home/sadik/NLM/shortly/shortly.py", line 45, in on_new_url
short_id = self.insert_url(url)
File "/home/sadik/NLM/shortly/shortly.py", line 72, in insert_url
short_id = self.redis.get('reverse-url:' + url)
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/client.py", line 705, in get
return self.execute_command('GET', name)
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/client.py", line 464, in execute_command
connection.send_command(*args)
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/connection.py", line 334, in send_command
self.send_packed_command(self.pack_command(*args))
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/connection.py", line 316, in send_packed_command
self.connect()
File "/usr/local/lib/python2.7/dist-packages/redis-2.9.1-py2.7.egg/redis/connection.py", line 253, in connect
raise ConnectionError(self._error_message(e))
ConnectionError: Error 111 connecting localhost:6379. Connection refused.
The error message indicates that there is something wrong with localhost:6379
The relevant part of code is here:
def create_app(redis_host='localhost', redis_port=6379, with_static=True):
app = Shortly({
'redis_host': redis_host,
'redis_port': redis_port
})
if with_static:
app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
'/static': os.path.join(os.path.dirname(__file__), 'static')
})
return app
if __name__ == '__main__':
from werkzeug.serving import run_simple
app = create_app()
run_simple('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)
That means of course that the server is running on localhost:5000. So why is there another port number in the create_app function? That confuses me a bit.

I'm not familiar with shortly or werkzeug but it looks like you're missing the redis server, install one using your favourite package manager and try again.

Related

Flask traceback only shows external libraries

I'm running a Flask app and get the following error message:
* Debugger is active!
* Debugger PIN: ******
127.0.0.1 - - [29/Dec/2022 12:21:53] "GET /authorization/****/authorize HTTP/1.1" 302 -
127.0.0.1 - - [29/Dec/2022 12:21:53] "GET /authorization/****/test HTTP/1.1" 500 -
Traceback (most recent call last):
File "******/lib/python3.10/site-packages/flask/app.py", line 2548, in __call__
return self.wsgi_app(environ, start_response)
File "******/lib/python3.10/site-packages/flask/app.py", line 2528, in wsgi_app
response = self.handle_exception(e)
File "******/lib/python3.10/site-packages/flask/app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "******/lib/python3.10/site-packages/flask/app.py", line 1823, in full_dispatch_request
return self.finalize_request(rv)
File "******/lib/python3.10/site-packages/flask/app.py", line 1844, in finalize_request
response = self.process_response(response)
File "******/lib/python3.10/site-packages/flask/app.py", line 2340, in process_response
self.session_interface.save_session(self, ctx.session, response)
File "******/lib/python3.10/site-packages/flask/sessions.py", line 409, in save_session
val = self.get_signing_serializer(app).dumps(dict(session)) # type: ignore
File "******/lib/python3.10/site-packages/itsdangerous/serializer.py", line 207, in dumps
payload = want_bytes(self.dump_payload(obj))
File "******/lib/python3.10/site-packages/itsdangerous/url_safe.py", line 53, in dump_payload
json = super().dump_payload(obj)
File "******/lib/python3.10/site-packages/itsdangerous/serializer.py", line 169, in dump_payload
return want_bytes(self.serializer.dumps(obj, **self.serializer_kwargs))
File "******/lib/python3.10/site-packages/flask/json/tag.py", line 308, in dumps
return dumps(self.tag(value), separators=(",", ":"))
File "******/lib/python3.10/site-packages/flask/json/__init__.py", line 124, in dumps
return app.json.dumps(obj, **kwargs)
File "******/lib/python3.10/site-packages/flask/json/provider.py", line 230, in dumps
return json.dumps(obj, **kwargs)
File "/usr/lib/python3.10/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.10/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "******/lib/python3.10/site-packages/flask/json/provider.py", line 122, in _default
raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")
I don't understand why it shows only lines from external libraries. What could be the reason it doesn't show which line in my code caused the program to stop?
When I try to replicate the error in a small app (code below), it does show the line number.
import flask
import json
app = flask.Flask(__name__)
app.secret_key = "####"
#app.route("/")
def index():
json.dumps(json) # Line that causes error
if __name__ == "__main__":
app.run("localhost", 8080, debug=True)
I think I understand what happened. The core of the error is
File "******/lib/python3.10/site-packages/flask/sessions.py", line 409, in save_session
val = self.get_signing_serializer(app).dumps(dict(session)) # type: ignore
This doesn't happen anywhere in my code, but rather somewhere later where Flask does things. And the thing it is doing is saving the session. If an error occurs there, it's because I put some improper thing in my session data. So the solution was to make sure that everything I save to flask.session is JSON serializable.

Flask logger does not work when served with an ASGI server like Uvicorn

I want to serve my Flask app with a ASGI server called Uvicorn. Turns out when I convert my app from wsgi to asgi (following instructions here https://flask.palletsprojects.com/en/2.0.x/deploying/asgi/) suddently app.logger function is undefined. But I really need the logging functionality to develop my app.
Please follow the steps below to reproduce the problem.
I wrote this basic app
from flask import Flask
from asgiref.wsgi import WsgiToAsgi
app = Flask(__name__)
#app.route("/")
def hello_world():
app.logger.info("hey I am your favourite log message")
return "<p>Hello, World!</p>"
app = WsgiToAsgi(app)
I installed uvicorn
pip install uvicorn
Ran my app
uvicorn app:app
And I get this error when the app tries to log using app.logger
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "f:\projects\logger-flask\venv\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 373, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "f:\projects\logger-flask\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\wsgi.py", line 21, in __call__
await WsgiToAsgiInstance(self.wsgi_application)(scope, receive, send)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\wsgi.py", line 51, in __call__
await self.run_wsgi_app(body)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\sync.py", line 444, in __call__
ret = await asyncio.wait_for(future, timeout=None)
File "C:\Users\Tamjid\AppData\Local\Programs\Python\Python38\lib\asyncio\tasks.py", line 455, in wait_for
return await fut
File "C:\Users\Tamjid\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\sync.py", line 486, in thread_handler
return func(*args, **kwargs)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\wsgi.py", line 140, in run_wsgi_app
for output in self.wsgi_application(environ, self.start_response):
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File ".\app.py", line 8, in hello_world
app.logger.info("hey I am your favourite log message")
AttributeError: 'WsgiToAsgi' object has no attribute 'logger'
An instance of Flask supports the WSGI protocol, and it also adds a number of conveniences, such as a logger. If you wrap the Flask instance and lose the reference to it, you lose those convenience methods.
Instead of
app = WsgiToAsgi(app)
try
wsgi = WsgiToAsgi(app)
then
uvicorn app:wsgi
I used different name for WsgiToAsgi instance and it removed the initial exception. But I was still getting a logging error.
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\Tamjid\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 1084, in emit
stream.write(msg + self.terminator)
TypeError: a bytes-like object is required, not 'str'
To solve this I added this two lines which removes the default logging handler
from flask.logging import default_handler
app.logger.removeHandler(default_handler)
My complete working code is here
from flask import Flask, current_app
from asgiref.wsgi import WsgiToAsgi
app = Flask(__name__)
from flask.logging import default_handler
app.logger.removeHandler(default_handler)
#app.route("/")
def hello_world():
app.logger.warning('hey I am your favourite log message')
return "<p>Hello, World!</p>"
wsgi = WsgiToAsgi(app)

AttributeError: module 'secrets' has no attribute 'token_bytes'

I am trying to set up a simple flask/socketio application however when I try and run it I get this error:
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask_socketio\__init__.py", line 45, in __call__
return super(_SocketIOMiddleware, self).__call__(environ,
return self.engineio_app.handle_request(environ, start_response)
return self.eio.handle_request(environ, start_response)
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\engineio\server.py", line 379, in handle_request
r = self._handle_connect(environ, start_response,
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\engineio\server.py", line 530, in _handle_connect
sid = self.generate_id()
File "C:\Users\tompi\AppData\Local\Programs\Python\Python39\Lib\site-packages\engineio\server.py", line 504, in generate_id
secrets.token_bytes(12) + self.sequence_number.to_bytes(3, 'big'))
AttributeError: module 'secrets' has no attribute 'token_bytes'
and I can't figure out why. Any ideas?
Here is the code:
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'abc'
socketio = SocketIO(app)
#app.route('/')
def index():
return 'hello world'
if __name__ == '__main__':
socketio.run(app, debug=True)
Something like this happened to me while I was executing black (the code formatter), even without arguments:
C:\GIT\myprogram>c:\Python310-64\python.exe -m black
Traceback (most recent call last):
File "c:\Python310-64\lib\runpy.py", line 187, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "c:\Python310-64\lib\runpy.py", line 146, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "c:\Python310-64\lib\runpy.py", line 110, in _get_module_details
__import__(pkg_name)
File "src\black\__init__.py", line 45, in <module>
File "c:\Python310-64\lib\site-packages\black\files.py", line 34, in <module>
from black.handle_ipynb_magics import jupyter_dependencies_are_installed
File "src\black\handle_ipynb_magics.py", line 49, in <module>
AttributeError: module 'secrets' has no attribute 'token_hex'
I hade a file named secrets.py in my current directory, which triggered this issue.

Python CGI with flask not working

I have just started shared hosting on godaddy starter pack.
I followed the instructions here to set up CGI with FLASK:
http://flask.pocoo.org/docs/deploying/cgi/
But when i run the cgi.py then i get this error:
Traceback (most recent call last):
File "/home/railwayapi/python/lib/python3.4/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/home/railwayapi/flasker/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/railwayapi/flasker/lib/python3.4/site-packages/flask/app.py", line 1812, in wsgi_app
ctx = self.request_context(environ)
File "/home/railwayapi/flasker/lib/python3.4/site-packages/flask/app.py", line 1773, in request_context
return RequestContext(self, environ)
File "/home/railwayapi/flasker/lib/python3.4/site-packages/flask/ctx.py", line 224, in __init__
self.url_adapter = app.create_url_adapter(self.request)
File "/home/railwayapi/flasker/lib/python3.4/site-packages/flask/app.py", line 1602, in create_url_adapter
server_name=self.config['SERVER_NAME'])
File "/home/railwayapi/flasker/lib/python3.4/site-packages/werkzeug/routing.py", line 1165, in bind_to_environ
server_name = environ['SERVER_NAME']
KeyError: 'SERVER_NAME'
Status: 500 Internal Server Error
Content-Type: text/plain
Content-Length: 59
This is the main part:
server_name = environ['SERVER_NAME']
KeyError: 'SERVER_NAME'
Why?
The .cgi file has to remain as it is mentioned in the Flask documentation.
Maybe you can refer to this -
https://medium.com/#mohdejazsiddiqui/deploy-flask-app-in-apache-shared-hosting-5b3c82c8fd5e

Celery Flask --- error: [Errno 111] Connection refused

After running through the basic example for flask-celery(runs fine as far as I can tell) I'm trying to integrate this to my own project. Basically, I'm using this below:
from flask import Blueprint, jsonify, request, session
from flask.views import MethodView
from celery.decorators import task
blueprint = Blueprint('myapi', __name__)
class MyAPI(MethodView):
def get(self, tag):
return get_resource.apply_async(tag)
#task(name="get_task")
def get_resource(tag):
pass
with the same setup as in the example, I'm getting this error:
Traceback (most recent call last):
File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/x/venv/lib/python2.7/site-packages/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/x/venv/lib/python2.7/site-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/x/venv/lib/python2.7/site-packages/flask/views.py", line 151, in dispatch_request
return meth(*args, **kwargs)
File "/x/api/modules/document/document.py", line 14, in get
return get_resource.apply_async(tag)
File "/x/venv/lib/python2.7/site-packages/celery/app/task/__init__.py", line 449, in apply_async
publish = publisher or self.app.amqp.publisher_pool.acquire(block=True)
File "/x/venv/lib/python2.7/site-packages/kombu/connection.py", line 657, in acquire
R = self.prepare(R)
File "/x/venv/lib/python2.7/site-packages/kombu/pools.py", line 54, in prepare
p = p()
File "/x/venv/lib/python2.7/site-packages/kombu/pools.py", line 45, in <lambda>
return lambda: self.create_producer()
File "/x/venv/lib/python2.7/site-packages/celery/app/amqp.py", line 265, in create_producer
pub = self.app.amqp.TaskPublisher(conn, auto_declare=False)
File "/x/venv/lib/python2.7/site-packages/celery/app/amqp.py", line 328, in TaskPublisher
return TaskPublisher(*args, **self.app.merge(defaults, kwargs))
File "/x/venv/lib/python2.7/site-packages/celery/app/amqp.py", line 158, in __init__
super(TaskPublisher, self).__init__(*args, **kwargs)
File "/x/venv/lib/python2.7/site-packages/kombu/compat.py", line 61, in __init__
super(Publisher, self).__init__(connection, self.exchange, **kwargs)
File "/x/venv/lib/python2.7/site-packages/kombu/messaging.py", line 79, in __init__
self.revive(self.channel)
File "/x/venv/lib/python2.7/site-packages/kombu/messaging.py", line 168, in revive
channel = channel.default_channel
File "/x/venv/lib/python2.7/site-packages/kombu/connection.py", line 581, in default_channel
self.connection
File "/x/venv/lib/python2.7/site-packages/kombu/connection.py", line 574, in connection
self._connection = self._establish_connection()
File "/x/venv/lib/python2.7/site-packages/kombu/connection.py", line 533, in _establish_connection
conn = self.transport.establish_connection()
File "/x/venv/lib/python2.7/site-packages/kombu/transport/amqplib.py", line 279, in establish_connection
connect_timeout=conninfo.connect_timeout)
File "/x/venv/lib/python2.7/site-packages/kombu/transport/amqplib.py", line 89, in __init__
super(Connection, self).__init__(*args, **kwargs)
File "/x/venv/lib/python2.7/site-packages/amqplib/client_0_8/connection.py", line 129, in __init__
self.transport = create_transport(host, connect_timeout, ssl)
File "/x/venv/lib/python2.7/site-packages/amqplib/client_0_8/transport.py", line 281, in create_transport
return TCPTransport(host, connect_timeout)
File "/x/venv/lib/python2.7/site-packages/amqplib/client_0_8/transport.py", line 85, in __init__
raise socket.error, msg
error: [Errno 111] Connection refused
-->
I'm using redis, and if I install rabbitmq I get another error, but I do not understand this right now --the broker should be redis but its isn't finding it or what? Can anyone give me more of a clue what is going on here? Do I need to import something else, etc. The point is, there is very little beyond the bare bones example and this makes no sense to me.
The most I've been able to determine as that in the Api module there is no access to the 'celery' and when it goes to try and put data there when at the app level, the celery there falls into some defaults, which aren't installed because I'm pointing to redis. Just a guess. I haven't been able to import information into the module, only determined that calling anything 'celery'(for example, output celery.conf) from the app causes an error -- although I could import celery.task.
This is the broker config the application is using, direct from the example:
BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = "redis"
CELERY_REDIS_HOST = "localhost"
CELERY_REDIS_PORT = 6379
CELERY_REDIS_DB = 0
EDIT:
If you'd like to see a demo: https://github.com/thrisp/flask-celery-example
AS it turns out having BROKER_TRANSPORT = 'redis' in your settings is important for whatever is being passed that I'm passing in (for the setup I put forth here and in the git example), I'm not entirely sure why it isn't in the example bits, but is in the ones I've added but it is -- without this it wants to dump everything onto a default ampq queue.
EDIT2:
Also, this a rather a big deal, using the upcoming version of Celery simplifies 10,000 issues when using it with Flask, making all of this unecesssary.
You must configure redis to bind the localhost. In /etc/redis/redis.conf, uncomment the line with
bind 127.0.0.1

Categories

Resources