3 P\@szdZddlZddlZddlZddlZddlmZddlmZddlmZddl m Z Gdd d ej Z Gd d d e ZdS) z/The implementation of the SocketOptionsAdapter.N)adapters) connection) poolmanager) exceptionscsdeZdZdZedk r0eejdejej dfgZ ngZ e j e jdfddZd fd d ZZS) SocketOptionsAdapteradAn adapter for requests that allows users to specify socket options. Since version 2.4.0 of requests, it is possible to specify a custom list of socket options that need to be set before establishing the connection. Example usage:: >>> import socket >>> import requests >>> from requests_toolbelt.adapters import socket_options >>> s = requests.Session() >>> opts = [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 0)] >>> adapter = socket_options.SocketOptionsAdapter(socket_options=opts) >>> s.mount('http://', adapter) You can also take advantage of the list of default options on this class to keep using the original options in addition to your custom options. In that case, ``opts`` might look like:: >>> opts = socket_options.SocketOptionsAdapter.default_options + opts NZdefault_socket_optionszThis version of Requests is only compatible with a version of urllib3 which is too old to support setting options on a socket. This adapter is functionally useless.c s&|jd|j|_tt|jf|dS)Nsocket_options)popdefault_optionsr superr__init__)selfkwargs) __class__$/usr/lib/python3.6/socket_options.pyr 5s zSocketOptionsAdapter.__init__Fcs:tjdkr"tj||||jd|_ntt|j|||dS)Ni)Z num_poolsmaxsizeblockr )requestsZ __build__rZ PoolManagerr r rinit_poolmanager)rZ connectionsrr)rrrr;s  z%SocketOptionsAdapter.init_poolmanager)F)__name__ __module__ __qualname____doc__rgetattrZHTTPConnectionsocket IPPROTO_TCPZ TCP_NODELAYr warningswarnexcZRequestsVersionTooOldr r __classcell__rr)rrrs rcs eZdZdZfddZZS)TCPKeepAliveAdapteraJAn adapter for requests that turns on TCP Keep-Alive by default. The adapter sets 4 socket options: - ``SOL_SOCKET`` ``SO_KEEPALIVE`` - This turns on TCP Keep-Alive - ``IPPROTO_TCP`` ``TCP_KEEPINTVL`` 20 - Sets the keep alive interval - ``IPPROTO_TCP`` ``TCP_KEEPCNT`` 5 - Sets the number of keep alive probes - ``IPPROTO_TCP`` ``TCP_KEEPIDLE`` 60 - Sets the keep alive time if the socket library has the ``TCP_KEEPIDLE`` constant The latter three can be overridden by keyword arguments (respectively): - ``idle`` - ``interval`` - ``count`` You can use this adapter like so:: >>> from requests_toolbelt.adapters import socket_options >>> tcp = socket_options.TCPKeepAliveAdapter(idle=120, interval=10) >>> s = requests.Session() >>> s.mount('http://', tcp) c s|jdtj}|jdd}|jdd}|jdd}|tjtjdfg}ttd ddk rl|tjtj|fg7}n(t j d krttd d }|tj||fg7}ttd ddk r|tjtj |fg7}ttdddk r|tjtj |fg7}t t|jfd|i|dS)Nr idle<intervalcountr TCP_KEEPINTVLdarwin TCP_KEEPALIVE TCP_KEEPCNT TCP_KEEPIDLE)r rr rZ SOL_SOCKETZ SO_KEEPALIVErrr)sysplatformr-r.r r"r )rrr r#r%r'r+)rrrr ds&       zTCPKeepAliveAdapter.__init__)rrrrr r!rr)rrr"Jsr")rrrr/rrZ_compatrrrr Z HTTPAdapterrr"rrrrs    ;