THRIFT-4767: Added TSocket/TSSLSocket support tcp keep-alive in python (#1725)

* THRIFT-4767: Added TSocket support tcp keep-alive in python
* TSSLSocket support tcp keep-alive
diff --git a/lib/py/src/transport/TSocket.py b/lib/py/src/transport/TSocket.py
index a7d6617..c8be25a 100644
--- a/lib/py/src/transport/TSocket.py
+++ b/lib/py/src/transport/TSocket.py
@@ -50,7 +50,9 @@
 class TSocket(TSocketBase):
     """Socket implementation of TTransport base."""
 
-    def __init__(self, host='localhost', port=9090, unix_socket=None, socket_family=socket.AF_UNSPEC):
+    def __init__(self, host='localhost', port=9090, unix_socket=None,
+                 socket_family=socket.AF_UNSPEC,
+                 socket_keepalive=False):
         """Initialize a TSocket
 
         @param host(str)  The host to connect to.
@@ -58,6 +60,7 @@
         @param unix_socket(str)  The filename of a unix socket to connect to.
                                  (host and port will be ignored.)
         @param socket_family(int)  The socket family to use with this socket.
+        @param socket_keepalive(bool) enable TCP keepalive, default off.
         """
         self.host = host
         self.port = port
@@ -65,6 +68,7 @@
         self._unix_socket = unix_socket
         self._timeout = None
         self._socket_family = socket_family
+        self._socket_keepalive = socket_keepalive
 
     def setHandle(self, h):
         self.handle = h
@@ -99,6 +103,11 @@
             raise TTransportException(TTransportException.NOT_OPEN, msg)
         for family, socktype, _, _, sockaddr in addrs:
             handle = self._do_open(family, socktype)
+
+            # TCP_KEEPALIVE
+            if self._socket_keepalive:
+                handle.setsockopt(socket.IPPROTO_TCP, socket.SO_KEEPALIVE, 1)
+
             handle.settimeout(self._timeout)
             try:
                 handle.connect(sockaddr)