I'm using older versions for one of my servers (because some of dependencies). I setup my server in docker container and its working fine until today. When i rebuild my image and re-run the service then its getting this error:
Exception on /messages [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1379, in handle_user_exception
return handler(e)
File "/opt/sync-engine/inbox/api/ns_api.py", line 226, in handle_input_error
request.environ['log_context']['error'] = error.__class__.__name__
KeyError: 'log_context'
{"module": "inbox.api.srv:28", "level": "error", "event": "Uncaught error thrown by Flask/Werkzeug", "timestamp": "2019-03-27T10:36:55.760581Z", "greenlet_id": 140055343249040}
srv.py (inbox.api.srv):
...
def default_json_error(ex):
""" Exception -> flask JSON responder """
logger = get_logger()
logger.error('Uncaught error thrown by Flask/Werkzeug', exc_info=ex)
response = jsonify(message=str(ex), type='api_error')
response.status_code = (ex.code
if isinstance(ex, HTTPException)
else 500)
return response
...
ns_api.py:
...
#app.errorhandler(APIException)
def handle_input_error(error):
# these "errors" are normal, so we don't need to save a traceback
request.environ['log_context']['error'] = error.__class__.__name__
request.environ['log_context']['error_message'] = error.message
response = flask_jsonify(message=error.message,
type='invalid_request_error')
response.status_code = error.status_code
return response
...
The main reason:
there is no log_context key in the request.environ
To fix this just initialize the log_context key
request.environ.setdefault('log_context', dict())
# etc...
request.environ['log_context']['error'] = error.__class__.__name__
Hope this helps.
Related
from google.cloud import tasks_v2
import json
GCP_PROJECT='test'
GCP_LOCATION='europe-west6'
def enqueue_task(queue_name, payload, process_url):
client = tasks_v2.CloudTasksClient()
parent = client.queue_path(GCP_PROJECT, GCP_LOCATION, queue_name)
task = {
'app_engine_http_request': {
'http_method': 'POST',
'relative_uri': process_url
}
}
if payload is None:
return False
payload = json.dumps(payload)
converted_payload = payload.encode()
task['app_engine_http_request']['body'] = converted_payload
return client.create_task(parent, task)
I keep getting the following error when I try to create a Google Cloud Task.
The Google App Engine Runtime used is python38. It was working fine but suddenly after deployment using gcp CLI it does not work now.
Traceback (most recent call last):
File "/layers/google.python.pip/pip/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/layers/google.python.pip/pip/flask/_compat.py", line 39, in reraise
raise value
File "/layers/google.python.pip/pip/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/layers/google.python.pip/pip/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/srv/main.py", line 153, in sync_request
queue_response = queues.enqueue_task(
File "/srv/queues.py", line 28, in enqueue_task
return client.create_task(parent, task)
TypeError: create_task() takes from 1 to 2 positional arguments but 3 were given
The same thing has just happened to me (on live server :/). Turns out, the Cloud Tasks library has changed significantly with version 2.0.0. You can read what you need to do to upgrade here.
Your problem is in this line:
client.create_task(parent, task)
The updated library requires to either use a dictionary as a positional argument or use keyword arguments. So this should fix it:
client.create_task(parent=parent, task=task)
EDIT: Looking at your code now that I've made this work for myself, you will also have to change the following:
# Before
parent = client.queue_path(GCP_PROJECT, GCP_LOCATION, queue_name)
# After
parent = client.queue_path(project=GCP_PROJECT, location=GCP_LOCATION, queue=queue_name)
and
# Before
'http_method': 'POST',
# After
'http_method': tasks_v2.HttpMethod.POST,
I have a python (flask) application that when I curl it using following command:
Request:
curl -X POST "localhost:8090/endp/" -H "Content-Type: application/json" -d #jsonExample
JsonEquals file includes:
{"idSeq":"0123","timeStamp":"15032019","source":"US","destination":"GB","content":"parcel"}
Response: Note: It will return two NEW fields which is as expected!
{
"idSeq": "0123",
"routeInfo": "Good",
"content": "parcel",
"Notes": "Send fine "
}
But when I run my python unittest using command: python myTest.py
import os, sys
import json
import main
import unittest
class PublicTestCase(unittest.TestCase):
def setUp(self):
self.app = main.app.test_client()
def my_first_test(self):
headers = {
'Content-Type': 'application/json'
}
data = {
"idSeq": "0123",
"timeStamp": "15032019",
"source": "US",
"destination": "GB",
"content": "parcel"
}
response = self.app.post('/endp',
headers=headers,
data=json.dumps(data),
follow_redirects=True)
print("+++++++++++++++++++++++++++++++++++")
print(data)
print("+++++++++++++++++++++++++++++++++++")
print(response)
assert response.status_code == 200
if __name__ == '__main__':
unittest.main()
I then get the following error:
[2020-03-05 14:00:08,093] ERROR in app: Exception on /endp/ [POST]
Traceback (most recent call last):
File "/home/ubuntu/venvs/inference/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/ubuntu/venvs/inference/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/ubuntu/venvs/inference/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/ubuntu/venvs/inference/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/ubuntu/venvs/inference/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/ubuntu/venvs/inference/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "../src/main.py", line 72, in mypostmethod
content = req_data['content']
TypeError: 'NoneType' object is not subscriptable
+++++++++++++++++++++++++++++++++++
{'idSeq': '0123', 'timeStamp': '15032019', 'source': 'US', 'destination': 'GB', 'content': 'parcel'}
+++++++++++++++++++++++++++++++++++
<Response streamed [500 INTERNAL SERVER ERROR]>
F
======================================================================
FAIL: my_first_test (__main__.PublicTestCase)
Any idea what I am doing wrong? Apologies if something similar was asked before but I checked and could not find a relevant answer to what I am doing with python FLASK!
main.py
#app.route("/endp/", methods=['POST'])
def mypostmethod():
req_data = request.get_json()
content = req_data['content']
timeStamp = req_data['timeStamp']
idSeq = req_data['idSeq']
...
...
Trying to use the google provided python sample code for adding a task to a Google Cloud Tasks queue. The docs from google are here: https://cloud.google.com/tasks/docs/creating-appengine-tasks
When I attempt to execute the endpoint I get the following error:
ImportError: cannot import name 'resource_pb2'
Has anyone seen an similar issue and know how to get past this?
Thanks in advance for your time :)
-nathan
I have tried adding google-cloud-tasks, google-api, google-cloud to my requirements.txt and running pip install google-cloud-tasks in my virtual environment. I also tried using this code:
from google.cloud import tasks_v2beta3
The results were the same
This is the example code provided by google. I have sent a note to them to let them know the example code does not work.
from google.cloud import tasks_v2
from google.protobuf import timestamp_pb2
# Create a client.
client = tasks_v2.CloudTasksClient()
# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# queue = 'my-appengine-queue'
# location = 'us-central1'
# payload = 'hello'
# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)
# Construct the request body.
task = {
'app_engine_http_request': { # Specify the type of request.
'http_method': 'POST',
'relative_uri': '/example_task_handler'
}
}
if payload is not None:
# The API expects a payload of type bytes.
converted_payload = payload.encode()
# Add the payload to the request.
task['app_engine_http_request']['body'] = converted_payload
if in_seconds is not None:
# Convert "seconds from now" into an rfc3339 datetime string.
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)
# Create Timestamp protobuf.
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(d)
# Add the timestamp to the tasks.
task['schedule_time'] = timestamp
# Use the client to build and send the task.
response = client.create_task(parent, task)
print('Created task {}'.format(response.name))
return response
I expected the code to add a task to the Cloud Tasks Queue, instead I get this stack trace:
Traceback (most recent call last)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/athena_nsunderman/posttest-get/main.py", line 134, in TestQueueInsert
from google.cloud import tasks_v2
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/google/cloud/tasks_v2/__init__.py", line 19, in <module>
from google.cloud.tasks_v2 import types
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/google/cloud/tasks_v2/types.py", line 22, in <module>
from google.cloud.tasks_v2.proto import cloudtasks_pb2
File "/home/athena_nsunderman/virtualenv/recharge/lib/python3.5/site-packages/google/cloud/tasks_v2/proto/cloudtasks_pb2.py", line 18, in <module>
from google.api import resource_pb2 as google_dot_api_dot_resource__pb2
ImportError: cannot import name 'resource_pb2'
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
This worked for me:
pip install -U googleapis-common-protos==1.5.10
When I try to access http://localhost:8000/apidocs/index.html I get the following error:
127.0.0.1 - - [31/Jan/2018 16:38:09] "GET /apidocs/index.html HTTP/1.1" 302 -
127.0.0.1 - - [31/Jan/2018 16:38:10] "GET /apidocs/ HTTP/1.1" 200 -
127.0.0.1 - - [31/Jan/2018 16:38:10] "GET /apispec_1.json HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python2.7/dist-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/views.py", line 149, in dispatch_request
return meth(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flasgger/base.py", line 205, in get
prefix_ids=prefix_ids)
File "/usr/local/lib/python2.7/dist-packages/flasgger/utils.py", line 648, in extract_definitions
properties.values(), level + 1, endpoint, verb, prefix_ids)
File "/usr/local/lib/python2.7/dist-packages/flasgger/utils.py", line 617, in extract_definitions
if not getattr(item, 'get'):
AttributeError: 'NoneType' object has no attribute 'get'
This is my flask application:
def init_deserializer_restful_api():
# Get port number for the web app.
PORT = 8000
# Initiate the Flask app
app = Flask(__name__)
Swagger(app)
CORS(app)
# Handler for deserializer
#app.route("/deserialize", methods=['POST'])
def handle_deserialization_request():
# here swagger documentation and content of method
"""
Deserialize a bag file to json
---
tags:
- deserializer
consumes:
- application/json
produces:
- application/json
parameters:
- name: body
in: body
description: request body
required: true
schema:
type: object
required:
- name
- es_addr
- json
properties:
name:
description: the file path to the log file / name of the file e.g log.tar.lz4
type: string
json:
description: local json file name
type: string
es_addr:
description: elastic search address
type: string
responses:
200:
description: returns the status of deserialization
schema:
type: object
properties:
status:
description: success / error
type: string
response:
description: message about the status
type: object
properties:
data:
description: the data returned by API upon success
type: string
error:
description: the error message returned by API upon failure
type: string
"""
# Check if input is valid json
if not request.json:
return jsonify(status="error",
response={"error": "Input is not JSON",
"data": ""})
else:
print("[INFO] Received log deserialization request")
parameters = request.get_json()
file_name = parameters.get("name")
es_addr = parameters.get("es_addr")
#es_index = parameters.get("es_index",file_name)
#buffer_size = parameters.get("buffer", 5000)
json_output = parameters.get("json")
#es_type = parameters.get("es_type","rosmsg")
# Run deserializer
ru = RosbagUploader(file_name, json_output, es_addr)
status = ru.start()
print("status = {}".format(status))
if status == ReturnCodes.SUCCESS:
return jsonify(status="success",
response={"error": "",
"data": ""})
else:
return jsonify(status="error",
response={"error": "Deserialization Failed",
"data": ""})
I don't understand the error, I tried adding GET next to POST above in my code did not solve the problem. I am not sure if the error is even from my code?
Any suggestions?
Be sure that your API specification is formatted properly when writing it as a docstring. You can write your specs to it's own file and use the file shortcut.
specs/deserialize.yml:
Deserialize a bag file to json
---
tags:
- deserializer
consumes:
- application/json
produces:
- application/json
parameters:
- name: body
in: body
description: request body
required: true
schema:
type: object
required:
- name
- es_addr
- json
properties:
name:
description: the file path to the log file / name of the file e.g log.tar.lz4
type: string
json:
description: local json file name
type: string
es_addr:
description: elastic search address
type: string
responses:
200:
description: returns the status of deserialization
schema:
type: object
properties:
status:
description: success / error
type: string
response:
description: message about the status
type: object
properties:
data:
description: the data returned by API upon success
type: string
error:
description: the error message returned by API upon failure
type: string
api.py:
def create_api():
# Get port number for the web app.
PORT = 8000
# Initiate the Flask app
app = Flask(__name__)
Swagger(app)
...
# Handler for deserializer
#app.route("/deserialize", methods=['POST'])
def handle_deserialization_request():
"""
file: specs/deserialize.yml
"""
...
return jsonify(
status="error",
response={"error": "Input is not JSON", "data": ""}
)
return app
api = init_deserializer_restful_api()
api.run()
I'm very new to python programming, I'm trying to expose few terminal commands as a http call. I'm packaging this below code into a tar.gz using setup.py and extracting this on the server to run the code in nginx web server. once I instantiate the app.py code using below, the http calls work fine for few hours. Not sure what happens after that, the http pages show an error and I do see the app is running in the server. Can someone please help me with this
gunicorn --bind 0.0.0.0:8000 cstaradmin:app &
#!/usr/bin/env python
from flask import Flask
from shelljob import proc
from flask import Response
from flask import json,jsonify
def get_pretty_print(json_object):
return json.dumps(json_object, sort_keys=False, indent=4, separators=('\n',' '))
def read_process(g):
while g.is_pending():
lines=g.readlines()
for proc,line in lines:
yield line
app = Flask(__name__)
def has_no_empty_params(rule):
defaults = rule.defaults if rule.defaults is not None else ()
arguments = rule.arguments if rule.arguments is not None else ()
return len(defaults) >= len(arguments)
#app.route('/', methods = ['GET'])
def help():
"""Print available functions."""
func_list = {}
for rule in app.url_map.iter_rules():
if rule.endpoint != 'static':
func_list[rule.rule] = app.view_functions[rule.endpoint].__doc__
return jsonify(func_list)
#app.route("/ps")
def IRR():
IRR = ["ps"]
g=proc.Group()
p=g.run(IRR)
return Response(read_process(g),mimetype= 'text/plain')
def catch_all(path):
return
if __name__ == "__main__":
try:
app.run(host='0.0.0.0',port=8000)
except:
pass
Error:
Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application
[2016-10-22 15:38:55,505] ERROR in app: Exception on /path/ToMy/command [GET]
Traceback (most recent call last):
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib64/python2.7/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/path/ToMyapp/admin/cstaradmin.py", line 42, in listnodes
p=g.run(listnodes)
File "/usr/lib/python2.7/site-packages/shelljob/proc.py", line 53, in run
CommandException( e, "Group.run '{}' failed".format( cmd ) ).do_raise()
File "/usr/lib/python2.7/site-packages/shelljob/proc.py", line 51, in run
return self._run_impl( cmd, shell )
File "/usr/lib/python2.7/site-packages/shelljob/proc.py", line 63, in _run_impl
stdin = subprocess.PIPE, # needed to detach from calling terminal (other wacky things can happen)
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1216, in _execute_child
errpipe_read, errpipe_write = self.pipe_cloexec()
File "/usr/lib64/python2.7/subprocess.py", line 1168, in pipe_cloexec
r, w = os.pipe()
CommandException: ("Group.run '['nodetool', ' status']' failed", OSError(24, 'Too many open files'))