comparison env/lib/python3.9/site-packages/requests_toolbelt/adapters/socket_options.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 # -*- coding: utf-8 -*-
2 """The implementation of the SocketOptionsAdapter."""
3 import socket
4 import warnings
5 import sys
6
7 import requests
8 from requests import adapters
9
10 from .._compat import connection
11 from .._compat import poolmanager
12 from .. import exceptions as exc
13
14
15 class SocketOptionsAdapter(adapters.HTTPAdapter):
16 """An adapter for requests that allows users to specify socket options.
17
18 Since version 2.4.0 of requests, it is possible to specify a custom list
19 of socket options that need to be set before establishing the connection.
20
21 Example usage::
22
23 >>> import socket
24 >>> import requests
25 >>> from requests_toolbelt.adapters import socket_options
26 >>> s = requests.Session()
27 >>> opts = [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 0)]
28 >>> adapter = socket_options.SocketOptionsAdapter(socket_options=opts)
29 >>> s.mount('http://', adapter)
30
31 You can also take advantage of the list of default options on this class
32 to keep using the original options in addition to your custom options. In
33 that case, ``opts`` might look like::
34
35 >>> opts = socket_options.SocketOptionsAdapter.default_options + opts
36
37 """
38
39 if connection is not None:
40 default_options = getattr(
41 connection.HTTPConnection,
42 'default_socket_options',
43 [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]
44 )
45 else:
46 default_options = []
47 warnings.warn(exc.RequestsVersionTooOld,
48 "This version of Requests is only compatible with a "
49 "version of urllib3 which is too old to support "
50 "setting options on a socket. This adapter is "
51 "functionally useless.")
52
53 def __init__(self, **kwargs):
54 self.socket_options = kwargs.pop('socket_options',
55 self.default_options)
56
57 super(SocketOptionsAdapter, self).__init__(**kwargs)
58
59 def init_poolmanager(self, connections, maxsize, block=False):
60 if requests.__build__ >= 0x020400:
61 # NOTE(Ian): Perhaps we should raise a warning
62 self.poolmanager = poolmanager.PoolManager(
63 num_pools=connections,
64 maxsize=maxsize,
65 block=block,
66 socket_options=self.socket_options
67 )
68 else:
69 super(SocketOptionsAdapter, self).init_poolmanager(
70 connections, maxsize, block
71 )
72
73
74 class TCPKeepAliveAdapter(SocketOptionsAdapter):
75 """An adapter for requests that turns on TCP Keep-Alive by default.
76
77 The adapter sets 4 socket options:
78
79 - ``SOL_SOCKET`` ``SO_KEEPALIVE`` - This turns on TCP Keep-Alive
80 - ``IPPROTO_TCP`` ``TCP_KEEPINTVL`` 20 - Sets the keep alive interval
81 - ``IPPROTO_TCP`` ``TCP_KEEPCNT`` 5 - Sets the number of keep alive probes
82 - ``IPPROTO_TCP`` ``TCP_KEEPIDLE`` 60 - Sets the keep alive time if the
83 socket library has the ``TCP_KEEPIDLE`` constant
84
85 The latter three can be overridden by keyword arguments (respectively):
86
87 - ``idle``
88 - ``interval``
89 - ``count``
90
91 You can use this adapter like so::
92
93 >>> from requests_toolbelt.adapters import socket_options
94 >>> tcp = socket_options.TCPKeepAliveAdapter(idle=120, interval=10)
95 >>> s = requests.Session()
96 >>> s.mount('http://', tcp)
97
98 """
99
100 def __init__(self, **kwargs):
101 socket_options = kwargs.pop('socket_options',
102 SocketOptionsAdapter.default_options)
103 idle = kwargs.pop('idle', 60)
104 interval = kwargs.pop('interval', 20)
105 count = kwargs.pop('count', 5)
106 socket_options = socket_options + [
107 (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
108 ]
109
110 # NOTE(Ian): OSX does not have these constants defined, so we
111 # set them conditionally.
112 if getattr(socket, 'TCP_KEEPINTVL', None) is not None:
113 socket_options += [(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL,
114 interval)]
115 elif sys.platform == 'darwin':
116 # On OSX, TCP_KEEPALIVE from netinet/tcp.h is not exported
117 # by python's socket module
118 TCP_KEEPALIVE = getattr(socket, 'TCP_KEEPALIVE', 0x10)
119 socket_options += [(socket.IPPROTO_TCP, TCP_KEEPALIVE, interval)]
120
121 if getattr(socket, 'TCP_KEEPCNT', None) is not None:
122 socket_options += [(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, count)]
123
124 if getattr(socket, 'TCP_KEEPIDLE', None) is not None:
125 socket_options += [(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, idle)]
126
127 super(TCPKeepAliveAdapter, self).__init__(
128 socket_options=socket_options, **kwargs
129 )