** Set up a server with the python ** web framework ** flask ** and point to the ** set up endpoint with some object ** (for example, a string, a list, an object of your own class) * A story when you want to send * **.
I think I often forget it every time, so I will summarize it here.
** Sender ** is
json.dumps()
I think it's easiest (for me) to send it.
The ** receiver ** receives the data and does the work done by the sender in reverse.
`json.loads ()`
to return from the string to the dictionary.The flow is like this. An overview. I often use the method of finally turning the object into a string, putting that string in a dict, json.dumps, and then sending it.
Sender.py
〜〜〜〜
What you want to send= something
case1 isinstance(What you want to send,Some class object): ## もしWhat you want to sendがオブジェクトのとき
if you want to send can be binalized: ##If the object can be binary
'pickle.dumps()Binary the object with(byte)Let's.'
my_binary = pickle.dumps(What you want to send)
GO_TO case2
else: ##If the object cannot be binary
'Make it binary in some way, or in the case of your own class object,
You need to write a serialize function. For example, a function that writes all the member variables of a class to a dictionary. For example, to_dict()Implement the function.'
my_binary =What you want to send.to_dict()
GO_TO case4
case2 isinstance(What you want to send, byte): ## もしWhat you want to sendがbyteのとき
'Base64 what you want to send.b64encode()Let's make it a character string with.'
my_str = base64.b64encode(What you want to send)
GO_TO case3
case3 isinstance(What you want to send, str): ## もしWhat you want to sendがstr(文字列)のとき
'Let's put it in the dictionary.'
my_dict = {"my_object":What you want to send}
GO_TO case4
case4 isinstance(What you want to send, dict): ## もしWhat you want to sendがすでにdictのとき
'What you want to send json.dumps()Then you're ready to go.'
What you want to send= json.dumps(What you want to send)
EXIT_CASE
assert type(What you want to send) =String(str), 'Please go back to the beginning.'
## print(What you want to send)
## >>> '{"my_object": my_value}'
requests.post(url="<Destination endpoint>",What you want to send,
headers={'Content-Type': 'application/json'})
Recipient.py
〜〜〜〜
@app.route('/receive')
def receive():
data = request.data ##"What you want to send" sent by post is request.You can receive it with data.
data = json.loads(data)['my_object'] ##First json.Return to the dictionary with loads,
##key is my_value of object(What was sent in the form of a string)To receive.
if data binary(byte)When you want to return to:
'base64 data.b64decode()Let's decode to byte with.'
data2 = base64.b64decode(data)
When you want to return elif data to a class object:
if pickle.When you can return with loads:
data3 = pickle.loads(data2)
else If pickle cannot be used with an object of your own class:
'Deserialization function by itself, dict_to_obj()Let's implement a function like this and revert to the original class object'
data3 = dict_to_obj(data2)
This time, I summarized how to post an object to the endpoint set up in flask.
This time around here.
end.