How to optimize Interface call by requests in python
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.