How to change tcp keepalive timer using python script?

You can set the TCP keepalive timers on an already-open socket using setsockopt(). import socket def set_keepalive_linux(sock, after_idle_sec=1, interval_sec=3, max_fails=5): “””Set TCP keepalive on an open socket. It activates after 1 second (after_idle_sec) of idleness, then sends a keepalive ping once every 3 seconds (interval_sec), and closes the connection after 5 failed ping (max_fails), or … Read more

Python urllib2 with keep alive

Use the urlgrabber library. This includes an HTTP handler for urllib2 that supports HTTP 1.1 and keepalive: >>> import urllib2 >>> from urlgrabber.keepalive import HTTPHandler >>> keepalive_handler = HTTPHandler() >>> opener = urllib2.build_opener(keepalive_handler) >>> urllib2.install_opener(opener) >>> >>> fo = urllib2.urlopen(‘http://www.python.org’) Note: you should use urlgrabber version 3.9.0 or earlier, as the keepalive module has been … Read more

HTTP keep-alive timeout

The client cannot specify the timeout, it is the server configuration that determines the maximum timeout value. The extra Keep-Alive header can inform the client how long the server is willing to keep the connection open (timeout=N value) and how many requests you can do over the same connection (max=M) before the server will force … Read more

python requests module and connection reuse

Global functions like requests.get or requests.post create the requests.Session instance on each call. Connections made with these functions cannot be reused, because you cannot access automatically created session and use it’s connection pool for subsequent requests. It’s fine to use these functions if you have to do just a few requests. Otherwise you’ll want to … Read more

Persistent/keepalive HTTP with the PHP Curl library?

cURL PHP documentation (curl_setopt) says: CURLOPT_FORBID_REUSE – TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse. So: Yes, actually it should re-use connections by default, as long as you re-use the cURL handle. by default, cURL handles persistent connections by itself; should you need some … Read more

How do I shutdown a Node.js http(s) server immediately?

The trick is that you need to subscribe to the server’s connection event which gives you the socket of the new connection. You need to remember this socket and later on, directly after having called server.close(), destroy that socket using socket.destroy(). Additionally, you need to listen to the socket’s close event to remove it from … Read more

http keep-alive in the modern age

Hey since I’m the author of this citation, I’ll respond 🙂 There are two big issues on large sites : concurrent connections and latency. Concurrent connection are caused by slow clients which take ages to download contents, and by idle connection states. Those idle connection states are caused by connection reuse to fetch multiple objects, … Read more