Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does the python backend connect with the python client

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report