Home > other >  Small white novice consult! Can't get access to the key in the dictionary!
Small white novice consult! Can't get access to the key in the dictionary!

Time:09-20

Small white novice, use the request. Get_data () to obtain the post up data, using the get take the value of the dictionary now to post up the data, but can't take the key dictionary, prompt msg_dict=sms_cord. Get (" MSG ")
AttributeError: 'bytes' object has no attribute to' get '

Backend before commissioning are normal, suddenly become so, don't know what the reason,

 
The from flask import flask, request, Response
The import json
The import base64
# import pymysql
# Flask framework core function
App=Flask (__name__)
# conn=pymysql. Connect (' localhost 'user="root", "=" deng @ 828416 db="sms_data")
# cursor=conn. Cursor ()
# cursor. The execute (' use sms_data; ')
# post parameters, the path to the back to add "/", or you'll get by default mode
@ app. The route ('/API/MSG/', the methods=[" GET ", "POST"])
Def test1 () :
# request. Get_json get Json data
Sms_cord=request. Get_data ()
Print (" sms_data: "+ format (sms_cord))
Sms_cord_sms=type (sms_cord)
Print (type (sms_cord_sms))
# get in through a dictionary for specified items specified in the parameter
Msg_dict=sms_cord. Get (" MSG ")
Src_dict=sms_cord. Get (" SRC ")
SubmitDate_dict=sms_cord. Get (" submitDate ")
MessageId_dict=sms_cord. Get (" messageId ")
Dest_dict=sms_cord. Get (" dest ")
# use base64 decoding module data
New_src=https://bbs.csdn.net/topics/base64.b64decode (src_dict. Encode (' utf-8 ')). The decode (" utf-8 ")
New_msg=base64. B64decode (msg_dict. Encode (' utf-8 ')). The decode (" utf-8 ")
New_dest=base64. B64decode (dest_dict. Encode (' utf-8 ')). The decode (" utf-8 ")
Print (" MSG: "+ STR (msg_dict))
Print (" SRC: "+ STR (src_dict))
Print (" submitDate: "+ STR (submitDate_dict))
Print (" messageId: "+ STR (messageId_dict))
Print (" dest: "+ STR (dest_dict))
# return value
Result={
"Result" : "0000"
}



Debug print:
Sms_data: b '{" MSG ", "44 cqvexjgje86im66b6zpumqjoivgeeggtk3mza5neeuqos6juazqowgjc/nmbvlvZXkuKrkurrotKblj7fnrYksNeWIhumSn + 35 yu WGheacieaViCzoibrpvpnlt6XkvZzkurrlkZjmiJblkIjkvZzllYbmiLfkuI3kvJrlkJHmgqjntKLopoHpqozor4HnoIEs6K +/5 zcr5lu75l2v5lq65roe6zyy", "SRC" : "MTA2OTEyNDY1NzM5MQ==", "submitDate" : "20200421171245", "messageId" : "1356220045", "dest" : "MTMwNDA3OTQyNjI="}'
192.168.5.119-23/Apr/2020 10:28:50 "POST/API/MSG/HTTP/1.1" 500 -
Traceback (the most recent call last) :
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ app. Py", line 2464, in __call__
Return the self. Wsgi_app (environ, start_response)
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ app. Py", line 2450, in wsgi_app
The response=self. Handle_exception (e)
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ app. Py", line 1867, in handle_exception
Reraise (exc_type exc_value, TB)
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ _compat py", line 39, reraise in
Raise the value
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ app. Py", line 2447, in wsgi_app
The response=self. Full_dispatch_request ()
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ app. Py", line 1952, in full_dispatch_request
The rv=self. Handle_user_exception (e)
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ app. Py", line 1821, in handle_user_exception
Reraise (exc_type exc_value, TB)
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ _compat py", line 39, reraise in
Raise the value
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ app. Py", line 1950, in full_dispatch_request
The rv=self. Dispatch_request ()
The File "E: \ PycharmProjects \ venv \ lib \ site - packages \ flask \ app. Py", line 1936, in dispatch_request
Return the self. View_functions [rule. The endpoint] (* * the req. View_args)
The File "E: \ PycharmProjects \ http_server. Py, 28, the line in test1
Msg_dict=sms_cord. Get (" MSG ")
AttributeError: 'bytes' object has no attribute to' get '

CodePudding user response:

Find the problem, request. Get_data () to obtain the string, json. Turn the dictionary loads

CodePudding user response:

reference 1st floor tomjimn response:
find the problem, request the get_data () to obtain the string, json. Loads turn to a dictionary


Congratulations to the building Lord, electing the building Lord!
  • Related