I am finding following error in code, Can someone please guide me why I am finding this error and how can I fix it.
I LOGGED response before its used in Response(json.dumps(response), content_type='application/json', charset='utf8'):-
[service_variant=lms][root][env:sandbox] WARNING [waleed-HP-Compaq-Elite-8300-CMT 32269] [mixins.py:75] - (json.dumps(response)-- {'url': u'/openassessment/fileupload/submissions_attachments/a419e1fff5e9a869a46d537c7ae3a18a/course-v1:edX+DemoX+Demo_Course/block-v1:edX+DemoX+Demo_Course+type#openassessment+block#051fae2bdc4f47929078eda8463a8c44/', 'success': True}
[service_variant=lms][courseware.module_render][env:sandbox] ERROR
[waleed-HP-Compaq-Elite-8300-CMT 32269] [module_render.py:1210] -
error executing xblock handler
Traceback (most recent call last):
File "/edx/app/edxapp/edx-platform/lms/djangoapps/courseware/module_render.py", line 1185, in _invoke_xblock_handler
resp = handler_instance.handle(handler, req, suffix)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/xblock/mixins.py", line 91, in handle
return self.runtime.handle(self, handler_name, request, suffix)
File "/edx/app/edxapp/edx-platform/common/lib/xmodule/xmodule/x_module.py", line 1365, in handle
return super(MetricsMixin, self).handle(block, handler_name, request, suffix=suffix)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/xblock/runtime.py", line 1037, in handle
results = handler(request, suffix)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/xblock/mixins.py", line 76, in wrapper
return Response(json.dumps(response), content_type='application/json', charset='utf8')
File "/usr/lib/python2.7/json/__init__.py", line 244, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
ERROR:-
TypeError: u'/openassessment/fileupload/submissions_attachments/a419e1fff5e9a869a46d537c7ae3a18a/course-v1:edX+DemoX+Demo_Course/block-v1:edX+DemoX+Demo_Course+type#openassessment+block#051fae2bdc4f47929078eda8463a8c44/' is not JSON serializable
Related
Here's the relevant code:
print(type(document))
myjson = json.dumps( document )
Here's what I get:
<class 'dict'>
Traceback (most recent call last):
File "./monitor.py", line 965, in <module>
myjson = json.dumps( nft )
File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Decimal is not JSON serializable
So type() thinks its a dict, which it is but dumps() thinks its a Decimal. Now what?
I tired assigning it to another variable thinking it was the name that was throwing things. No difference. type() thinks its a dict, dumps() thinks its a Decimal.
I am not able to tell what item I touched inmy code. There is this traceback for an error. May somebody help me find the problem and how to solve it.
Traceback (most recent call last):
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\deprecation.py", line 119, in __call__
response = self.process_response(request, response)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\sessions\middleware.py", line 61, in process_response
request.session.save()
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\sessions\backends\db.py", line 83, in save
obj = self.create_model_instance(data)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\sessions\backends\db.py", line 70, in create_model_instance
session_data=self.encode(data),
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\sessions\backends\base.py", line 114, in encode
return signing.dumps(
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\signing.py", line 110, in dumps
return TimestampSigner(key, salt=salt).sign_object(obj, serializer=serializer, compress=compress)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\signing.py", line 172, in sign_object
data = serializer().dumps(obj)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\signing.py", line 87, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 234, in dumps
return cls(
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type QuerySet is not JSON serializable
I am trying to use connexion/Flask to return a base64-encoded image generated by PIL/pillow in a HTTP Response.
Having upgraded to python3.7 from python2.7, that part of the code works unaltered.
However, when running inside Docker (python3.7-slim), I get the time-honoured TypeError: Object of type bytes is not JSON serializable (full Traceback below).
The encoding is undertaken via
cutout_b64 = base64.b64encode(sbuffer.getvalue()).decode("ascii")
PLEASE NOTE: There are many many many answers that suggest applying .decode(...) but this does not work inside Docker! The question is:
Q. Why does this work outside Docker but not inside the container? How do I fix this? Is it a LANG difference perhaps?
Thanks as ever
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.7/site-packages/connexion/decorators/decorator.py", line 68, in wrapper
response = function(request)
File "/usr/local/lib/python3.7/site-packages/connexion/decorators/uri_parsing.py", line 149, in wrapper
response = function(request)
File "/app/custom_validators.py", line 107, in wrapper
response = function(request)
File "/usr/local/lib/python3.7/site-packages/connexion/decorators/response.py", line 110, in wrapper
return _wrapper(request, response)
File "/usr/local/lib/python3.7/site-packages/connexion/decorators/response.py", line 90, in _wrapper
self.operation.api.get_connexion_response(response, self.mimetype)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 365, in get_connexion_response
response = cls._response_from_handler(response, mimetype)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 345, in _response_from_handler
return cls._build_response(mimetype=mimetype, data=response, extra_context=extra_context)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/flask_api.py", line 186, in _build_response
data, status_code, serialized_mimetype = cls._prepare_body_and_status_code(data=data, mimetype=mimetype, status_code=status_code, extra_context=extra_context)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 417, in _prepare_body_and_status_code
body, mimetype = cls._serialize_data(data, mimetype)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/flask_api.py", line 203, in _serialize_data
body = cls.jsonifier.dumps(data)
File "/usr/local/lib/python3.7/site-packages/connexion/jsonifier.py", line 57, in dumps
return self.json.dumps(data, **kwargs) + '\n'
File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 211, in dumps
rv = _json.dumps(obj, **kwargs)
File "/usr/local/lib/python3.7/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/local/lib/python3.7/json/encoder.py", line 201, in encode
chunks = list(chunks)
File "/usr/local/lib/python3.7/json/encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/local/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/local/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/local/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/local/lib/python3.7/json/encoder.py", line 438, in _iterencode
o = _default(o)
File "/usr/local/lib/python3.7/site-packages/connexion/apps/flask_app.py", line 146, in default
return json.JSONEncoder.default(self, o)
File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 100, in default
return _json.JSONEncoder.default(self, o)
File "/usr/local/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
In the main function, i call a function in which another application is called, as a result i get data in json format.
But I don’t understand where the slashes come from in front of each double quotation "
in the browser, I see data with quotes
Example: {\"192.168.43.1\":[\"53\":{\"state\":\"open\"...
If I do not send the data but write to the file, data is written to the file WITHOUT slashes
Example: {"192.168.43.1":["53":{"state":"open"...
This is normal?
How do I remove the slashes?
This data must accept
another application and deserialize them.
def get_ip(ip, port):
return os.system("some_app")
#hug.get('/scan')
def main(ip: hug.types.text, port: hug.types.text):
json = get_ip(ip, port)
#JUST FOR TEST WITH PARAM safe=False
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}", safe=False)
Errors without parametr safe=False:
Traceback (most recent call last):
File "/usr/lib/python3.6/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python3.6/dist-packages/falcon/api.py", line 244, in __call__
responder(req, resp, **params)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 793, in __call__
raise exception
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 766, in __call__
self.render_content(self.call_function(input_parameters), context, request, response, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 703, in call_function
return self.interface(**parameters)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 100, in __call__
return __hug_internal_self._function(*args, **kwargs)
File "script.py", line 181, in main
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}")
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 552, in __init__
'In order to allow non-dict objects to be serialized set the '
TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False.
Errors with parametr safe=False:
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}", safe=False)
File "/usr/lib/python3.6/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python3.6/dist-packages/falcon/api.py", line 244, in __call__
responder(req, resp, **params)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 793, in __call__
raise exception
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 766, in __call__
self.render_content(self.call_function(input_parameters), context, request, response, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 703, in call_function
return self.interface(**parameters)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 100, in __call__
return __hug_internal_self._function(*args, **kwargs)
File "script.py", line 181, in main
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}", safe=False)
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 559, in __init__
super().__init__(content=data, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 291, in __init__
self.content = content
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 321, in content
content = self.make_bytes(value)
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 236, in make_bytes
return bytes(value.encode(self.charset))
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 85, in charset
return settings.DEFAULT_CHARSET
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 57, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting
DEFAULT_CHARSET, but settings are not configured. You must either define the
environment variable DJANGO_SETTINGS_MODULE or call settings.configure()
before accessing settings.
Your return "result {json}" would return string value which contains json. Since returned value is string, browser shows you extra backslashes in order to properly handle double quotes.
To solve this problem you could handle string response in client side and extract json values from string.
JSON.parse(response);
But since another application is expecting json format you'd better to use JsonResponse to make sure return response is not string but json format
from django.http import JsonResponse
return JsonResponse(json)
I can't for the life of me figure out why I can't catch this exception.
Looking here at this guide.
def get_team_names(get_team_id_url, team_id):
print(get_team_id_url + team_id)
try:
response = urllib.request.urlopen(get_team_id_url + team_id)
except urllib.error.HTTPError as e:
print(e.code)
print(e.read())
except urllib.error.URLError as e:
print(e.code)
print(e.read())
exception:
Traceback (most recent call last):
File "queue_cleaner_main.py", line 60, in <module>
sys.exit(main())
File "queue_cleaner_main.py", line 57, in main
team_names_to_contact = queue_cleaner_functions.get_team_names(SERVICE_NOW_TEAM_NAME_URL, team[2])
File "D:\oppssup\old_job\queue_cleaner_functions.py", line 132, in get_team_names
response = urllib.request.urlopen(get_team_id_url + team_id)
File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 455, in open
response = self._open(req, data)
File "C:\Python34\lib\urllib\request.py", line 473, in _open
'_open', req)
File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
result = func(*args)
File "C:\Python34\lib\urllib\request.py", line 1202, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Python34\lib\urllib\request.py", line 1177, in do_open
r = h.getresponse()
File "C:\Python34\lib\http\client.py", line 1172, in getresponse
response.begin()
File "C:\Python34\lib\http\client.py", line 351, in begin
version, status, reason = self._read_status()
File "C:\Python34\lib\http\client.py", line 321, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: ''
In addition, my entire method seems to be very clunky. I cannot figure out an elegant way to handle exceptions, including a case where no result is returned.
def get_team_names(get_team_id_url, team_id):
print(get_team_id_url + team_id)
try:
response = urllib.request.urlopen(get_team_id_url + team_id)
except Exception as e:
#print(e.code)
#print(e.read())
print('shit')
#print(response.read().decode('utf8'))
r_json = json.loads(response.read().decode('utf8'))
print(r_json['result'])
for i in r_json['result']:
print(i['group'])
Exception:
Traceback (most recent call last):
File "queue_cleaner_main.py", line 60, in <module>
sys.exit(main())
File "queue_cleaner_main.py", line 57, in main
team_names_to_contact = queue_cleaner_functions.get_team_names(SERVICE_NOW_TEAM_NAME_URL, team[2])
File "D:\oppssup\old_job\queue_cleaner_functions.py", line 141, in get_team_names
r_json = json.loads(response.read().decode('utf8'))
UnboundLocalError: local variable 'response' referenced before assignment
This http.client.BadStatusLine is a subclass of http.client.HTTPException. I think if you do:
try:
response = urllib.request.urlopen(get_team_id_url + team_id)
except http.client.HTTPException as e:
print(e)
then you shouldn't have problem catching it. However, what caused it is perhaps what you should concern.