Python http persistent connection client and python

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.