Python http persistent connection client and python
Background:
For online machines, you need to filter access logs and send them to another api.
It is a single process at the beginning, and the efficiency is too low. After sending a message to a multi-process, an exception or error may occasionally occur in the log (forgot ...)
In short, an error is reported when the port is not enough.
Cause:
Each log is sent to the api once. Transient connections generate a large number of time_wait statuses, occupying a large number of ports.
This high concurrency caused a large number of time_wait status Kernel Tuning is useless, and later changed to a persistent connection to solve the problem.
The key code for the first short connection version is as follows:
Because it involves specific business information, only the key part of the code is pasted.
Import pycurlwhere True: url = myqueue. get () send_msg = pycurl. Curl () send_msg.setopt (pycurl. URL, url) send_msg.perform () print send_msg.getinfo (send_msg.HTTP_CODE)
The modified persistent connection version is as follows:
Use the requests Library
Import requestsclient = requests. session () headers = {'content-type': 'application/json', 'connection': 'Keep-alive'} where True: url = myqueue. get () r = client. get (url, headers = headers) print r. status_code
The above detailed discussion about the python http persistent Connection Client is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's home.