@@ -230,6 +230,10 @@ class Connection(BaseConnection):
230230 Set to true to check the server's identity.
231231 tls_sni_servername: str, optional
232232 Set server host name for TLS connection
233+ socket_options: Dict[int, Dict[int, any]], optional
234+ A dictionary of socket options to set on the connection.
235+ The keys are the socket level constants (e.g., socket.SOL_SOCKET),
236+ and the values are dictionaries mapping option names to values.
233237 read_default_group : str, optional
234238 Group to read from in the configuration file.
235239 autocommit : bool, optional
@@ -341,6 +345,7 @@ def __init__( # noqa: C901
341345 ssl_verify_cert = None ,
342346 ssl_verify_identity = None ,
343347 tls_sni_servername = None ,
348+ socket_options = None ,
344349 parse_json = True ,
345350 invalid_values = None ,
346351 pure_python = None ,
@@ -477,7 +482,7 @@ def _config(key, arg):
477482 self .collation = collation
478483 self .use_unicode = use_unicode
479484 self .encoding_errors = encoding_errors
480-
485+ self . _socket_options = socket_options or {}
481486 self .encoding = charset_by_name (self .charset ).encoding
482487
483488 client_flag |= CLIENT .CAPABILITIES
@@ -1107,6 +1112,11 @@ def connect(self, sock=None):
11071112 print ('connected using socket' )
11081113 sock .setsockopt (socket .IPPROTO_TCP , socket .TCP_NODELAY , 1 )
11091114 sock .setsockopt (socket .SOL_SOCKET , socket .SO_KEEPALIVE , 1 )
1115+
1116+ for level , options in self ._socket_options .items ():
1117+ for opt , value in options .items ():
1118+ sock .setsockopt (level , opt , value )
1119+
11101120 sock .settimeout (None )
11111121
11121122 self ._sock = sock
0 commit comments