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 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

How to keep an iPhone app running on background fully operational

From ioS 7 onwards, there are newer ways for apps to run in background. Apple now recognizes that apps have to constantly download and process data constantly. Here is the new list of all the apps which can run in background. Apps that play audible content to the user while in the background, such as … 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

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