In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to optimize the interface call of requests in python. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
When using Python for network programming, the requests module is often used to call the http interface.
1. If there are only a few interface calls, using the traditional requests.post () or requests.get () can meet the requirements.
However, if multiple interface calls are involved, you may encounter the problem of long execution time of the program.
2. In order to improve the execution efficiency of the program and reduce the pressure on the server, long connections can be used to save frequent tcp connection handshakes and waving.
Example
Def keep_alive (): "" instantiate a persistent connection object: return: "s = requests.Session () return s def q_inst (func, obj_id='host', field='bk_host_innerip', value=''): headers = {" Content-Type ":" application/json "} data = {" bk_obj_id ": obj_id," bk_supplier_account ":" 0 " "page": {"start": 0, "limit": 10}, "condition": {obj_id: [{"field": field, "operator": "$eq" "value": value}]} res_p = func.post ('https://***.***.***/api/c/compapi/v2/cc/search_inst/', data = data, headers = headers) return res_p if _ _ name__ = =' _ main__':s = keep_alive () for in range (4001): res = q_inst (s) Value='10.1.1.1') # output## query API takes a total of time: 0python 03python 21.099682 on "how to optimize the interface call in requests" this article ends here Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.