Get the App
SLTechnology News&Howtos  ›  Development  › 

How does the python backend connect with the python client

Shulou Source: shulou.com Published: 2022-06-01 13:20:22 11月03日 Update

This article introduces the knowledge of "how to connect the python backend to the python client". Many people will encounter this dilemma in the operation of the actual case, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Back-end processing

Json has been regulated to some extent.

From flask import Flaskimport jsonapp = Flask (_ _ name__) @ app.route ('/ hello') # specifies url and executes the annotated function def hello_world () when the requested url is / hello: # in python, the data format corresponding to json is a dictionary So we create a dictionary here to store data and return data = {'status':' 200, 'message':' ok', 'no':' 1 printing, 'name':' W3C Schoolgrounds, 'url':' http://www.yisu.com'} print (type (data)) # print Make sure the data type is a dictionary json_str = json.dumps (data) # use dumps to convert the dictionary type to a string, so that it can be returned through the http protocol # json is transmitted as a string return json_str # using flask, you can return this string directly with return You can return json to if _ _ name__ = ='_ _ main__': app.run () # to run the flask project client processing

Convert the requested link to our back-end server link. At the same time, the returned data is simply processed and displayed with python.

Import requestsimport jsonresponse = requests.get ('http://127.0.0.1:5000/hello')# uses request to request a jsonresponse = response.text# uses the text method of requests to fetch the text of the response dict = json.loads (response) # using the loads method of the JSON module, you can convert this string into a dictionary print (dict) # print this dictionary print (type (dict)) # confirm whether it is a dictionary type # here is the dictionary operation method Take out the data from the dictionary and print print ('response status:' + dict.get ('status')) print (' return message:'+ dict.get ('message')) print (' number:'+ dict.get ('no')) print (' website name:'+ dict.get ('name')) print (' website url:'+dict.get ('url')), "how the python backend connects with the python client". Thank you for reading here. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Dictionaries data characters strings clients clients methods types websites processing content more knowledge links transmission practical learned and then information functions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple Shulou Information Docker OPPO Reno