webull-openapi-python-sdk 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- samples/__init__.py +1 -0
- samples/data/__init__.py +1 -0
- samples/data/data_client.py +57 -0
- samples/data/data_streaming_client.py +86 -0
- samples/data/data_streaming_client_async.py +101 -0
- samples/trade/__init__.py +0 -0
- samples/trade/trade_client.py +163 -0
- samples/trade/trade_client_v2.py +181 -0
- samples/trade/trade_event_client.py +47 -0
- webull/__init__.py +1 -0
- webull/core/__init__.py +12 -0
- webull/core/auth/__init__.py +0 -0
- webull/core/auth/algorithm/__init__.py +0 -0
- webull/core/auth/algorithm/sha_hmac1.py +65 -0
- webull/core/auth/algorithm/sha_hmac256.py +75 -0
- webull/core/auth/composer/__init__.py +0 -0
- webull/core/auth/composer/default_signature_composer.py +125 -0
- webull/core/auth/credentials.py +46 -0
- webull/core/auth/signers/__init__.py +0 -0
- webull/core/auth/signers/app_key_signer.py +72 -0
- webull/core/auth/signers/signer.py +48 -0
- webull/core/auth/signers/signer_factory.py +58 -0
- webull/core/cache/__init__.py +225 -0
- webull/core/client.py +410 -0
- webull/core/common/__init__.py +0 -0
- webull/core/common/api_type.py +19 -0
- webull/core/common/easy_enum.py +35 -0
- webull/core/common/region.py +7 -0
- webull/core/compat.py +85 -0
- webull/core/context/__init__.py +0 -0
- webull/core/context/request_context_holder.py +33 -0
- webull/core/data/endpoints.json +22 -0
- webull/core/data/retry_config.json +15 -0
- webull/core/endpoint/__init__.py +8 -0
- webull/core/endpoint/chained_endpoint_resolver.py +57 -0
- webull/core/endpoint/default_endpoint_resolver.py +60 -0
- webull/core/endpoint/local_config_regional_endpoint_resolver.py +77 -0
- webull/core/endpoint/resolver_endpoint_request.py +46 -0
- webull/core/endpoint/user_customized_endpoint_resolver.py +55 -0
- webull/core/exception/__init__.py +0 -0
- webull/core/exception/error_code.py +23 -0
- webull/core/exception/error_msg.py +21 -0
- webull/core/exception/exceptions.py +53 -0
- webull/core/headers.py +57 -0
- webull/core/http/__init__.py +0 -0
- webull/core/http/initializer/__init__.py +0 -0
- webull/core/http/initializer/client_initializer.py +79 -0
- webull/core/http/initializer/token/__init__.py +0 -0
- webull/core/http/initializer/token/bean/__init__.py +0 -0
- webull/core/http/initializer/token/bean/access_token.py +40 -0
- webull/core/http/initializer/token/bean/check_token_request.py +44 -0
- webull/core/http/initializer/token/bean/create_token_request.py +45 -0
- webull/core/http/initializer/token/bean/refresh_token_request.py +44 -0
- webull/core/http/initializer/token/token_manager.py +208 -0
- webull/core/http/initializer/token/token_operation.py +72 -0
- webull/core/http/method_type.py +43 -0
- webull/core/http/protocol_type.py +43 -0
- webull/core/http/request.py +121 -0
- webull/core/http/response.py +166 -0
- webull/core/request.py +278 -0
- webull/core/retry/__init__.py +0 -0
- webull/core/retry/backoff_strategy.py +102 -0
- webull/core/retry/retry_condition.py +214 -0
- webull/core/retry/retry_policy.py +63 -0
- webull/core/retry/retry_policy_context.py +51 -0
- webull/core/utils/__init__.py +0 -0
- webull/core/utils/common.py +62 -0
- webull/core/utils/data.py +25 -0
- webull/core/utils/desensitize.py +33 -0
- webull/core/utils/validation.py +49 -0
- webull/core/vendored/__init__.py +0 -0
- webull/core/vendored/requests/__init__.py +94 -0
- webull/core/vendored/requests/__version__.py +28 -0
- webull/core/vendored/requests/_internal_utils.py +56 -0
- webull/core/vendored/requests/adapters.py +539 -0
- webull/core/vendored/requests/api.py +166 -0
- webull/core/vendored/requests/auth.py +307 -0
- webull/core/vendored/requests/certs.py +34 -0
- webull/core/vendored/requests/compat.py +85 -0
- webull/core/vendored/requests/cookies.py +555 -0
- webull/core/vendored/requests/exceptions.py +136 -0
- webull/core/vendored/requests/help.py +134 -0
- webull/core/vendored/requests/hooks.py +48 -0
- webull/core/vendored/requests/models.py +960 -0
- webull/core/vendored/requests/packages/__init__.py +17 -0
- webull/core/vendored/requests/packages/certifi/__init__.py +17 -0
- webull/core/vendored/requests/packages/certifi/__main__.py +16 -0
- webull/core/vendored/requests/packages/certifi/cacert.pem +4433 -0
- webull/core/vendored/requests/packages/certifi/core.py +51 -0
- webull/core/vendored/requests/packages/chardet/__init__.py +53 -0
- webull/core/vendored/requests/packages/chardet/big5freq.py +400 -0
- webull/core/vendored/requests/packages/chardet/big5prober.py +61 -0
- webull/core/vendored/requests/packages/chardet/chardistribution.py +247 -0
- webull/core/vendored/requests/packages/chardet/charsetgroupprober.py +120 -0
- webull/core/vendored/requests/packages/chardet/charsetprober.py +159 -0
- webull/core/vendored/requests/packages/chardet/cli/__init__.py +1 -0
- webull/core/vendored/requests/packages/chardet/cli/chardetect.py +99 -0
- webull/core/vendored/requests/packages/chardet/codingstatemachine.py +102 -0
- webull/core/vendored/requests/packages/chardet/compat.py +48 -0
- webull/core/vendored/requests/packages/chardet/cp949prober.py +63 -0
- webull/core/vendored/requests/packages/chardet/enums.py +90 -0
- webull/core/vendored/requests/packages/chardet/escprober.py +115 -0
- webull/core/vendored/requests/packages/chardet/escsm.py +260 -0
- webull/core/vendored/requests/packages/chardet/eucjpprober.py +106 -0
- webull/core/vendored/requests/packages/chardet/euckrfreq.py +209 -0
- webull/core/vendored/requests/packages/chardet/euckrprober.py +61 -0
- webull/core/vendored/requests/packages/chardet/euctwfreq.py +401 -0
- webull/core/vendored/requests/packages/chardet/euctwprober.py +60 -0
- webull/core/vendored/requests/packages/chardet/gb2312freq.py +297 -0
- webull/core/vendored/requests/packages/chardet/gb2312prober.py +60 -0
- webull/core/vendored/requests/packages/chardet/hebrewprober.py +306 -0
- webull/core/vendored/requests/packages/chardet/jisfreq.py +339 -0
- webull/core/vendored/requests/packages/chardet/jpcntx.py +247 -0
- webull/core/vendored/requests/packages/chardet/langbulgarianmodel.py +242 -0
- webull/core/vendored/requests/packages/chardet/langcyrillicmodel.py +347 -0
- webull/core/vendored/requests/packages/chardet/langgreekmodel.py +239 -0
- webull/core/vendored/requests/packages/chardet/langhebrewmodel.py +214 -0
- webull/core/vendored/requests/packages/chardet/langhungarianmodel.py +239 -0
- webull/core/vendored/requests/packages/chardet/langthaimodel.py +213 -0
- webull/core/vendored/requests/packages/chardet/langturkishmodel.py +207 -0
- webull/core/vendored/requests/packages/chardet/latin1prober.py +159 -0
- webull/core/vendored/requests/packages/chardet/mbcharsetprober.py +105 -0
- webull/core/vendored/requests/packages/chardet/mbcsgroupprober.py +68 -0
- webull/core/vendored/requests/packages/chardet/mbcssm.py +586 -0
- webull/core/vendored/requests/packages/chardet/sbcharsetprober.py +146 -0
- webull/core/vendored/requests/packages/chardet/sbcsgroupprober.py +87 -0
- webull/core/vendored/requests/packages/chardet/sjisprober.py +106 -0
- webull/core/vendored/requests/packages/chardet/universaldetector.py +300 -0
- webull/core/vendored/requests/packages/chardet/utf8prober.py +96 -0
- webull/core/vendored/requests/packages/chardet/version.py +23 -0
- webull/core/vendored/requests/packages/urllib3/__init__.py +114 -0
- webull/core/vendored/requests/packages/urllib3/_collections.py +346 -0
- webull/core/vendored/requests/packages/urllib3/connection.py +405 -0
- webull/core/vendored/requests/packages/urllib3/connectionpool.py +910 -0
- webull/core/vendored/requests/packages/urllib3/contrib/__init__.py +0 -0
- webull/core/vendored/requests/packages/urllib3/contrib/_appengine_environ.py +44 -0
- webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/__init__.py +0 -0
- webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/bindings.py +607 -0
- webull/core/vendored/requests/packages/urllib3/contrib/_securetransport/low_level.py +360 -0
- webull/core/vendored/requests/packages/urllib3/contrib/appengine.py +303 -0
- webull/core/vendored/requests/packages/urllib3/contrib/ntlmpool.py +125 -0
- webull/core/vendored/requests/packages/urllib3/contrib/pyopenssl.py +484 -0
- webull/core/vendored/requests/packages/urllib3/contrib/securetransport.py +818 -0
- webull/core/vendored/requests/packages/urllib3/contrib/socks.py +206 -0
- webull/core/vendored/requests/packages/urllib3/exceptions.py +260 -0
- webull/core/vendored/requests/packages/urllib3/fields.py +192 -0
- webull/core/vendored/requests/packages/urllib3/filepost.py +112 -0
- webull/core/vendored/requests/packages/urllib3/packages/__init__.py +19 -0
- webull/core/vendored/requests/packages/urllib3/packages/backports/__init__.py +0 -0
- webull/core/vendored/requests/packages/urllib3/packages/backports/makefile.py +67 -0
- webull/core/vendored/requests/packages/urllib3/packages/ordered_dict.py +273 -0
- webull/core/vendored/requests/packages/urllib3/packages/six.py +882 -0
- webull/core/vendored/requests/packages/urllib3/packages/socks.py +887 -0
- webull/core/vendored/requests/packages/urllib3/packages/ssl_match_hostname/__init__.py +19 -0
- webull/core/vendored/requests/packages/urllib3/packages/ssl_match_hostname/_implementation.py +170 -0
- webull/core/vendored/requests/packages/urllib3/poolmanager.py +467 -0
- webull/core/vendored/requests/packages/urllib3/request.py +164 -0
- webull/core/vendored/requests/packages/urllib3/response.py +721 -0
- webull/core/vendored/requests/packages/urllib3/util/__init__.py +68 -0
- webull/core/vendored/requests/packages/urllib3/util/connection.py +148 -0
- webull/core/vendored/requests/packages/urllib3/util/queue.py +35 -0
- webull/core/vendored/requests/packages/urllib3/util/request.py +132 -0
- webull/core/vendored/requests/packages/urllib3/util/response.py +101 -0
- webull/core/vendored/requests/packages/urllib3/util/retry.py +426 -0
- webull/core/vendored/requests/packages/urllib3/util/selectors.py +601 -0
- webull/core/vendored/requests/packages/urllib3/util/ssl_.py +396 -0
- webull/core/vendored/requests/packages/urllib3/util/timeout.py +256 -0
- webull/core/vendored/requests/packages/urllib3/util/url.py +252 -0
- webull/core/vendored/requests/packages/urllib3/util/wait.py +164 -0
- webull/core/vendored/requests/packages.py +28 -0
- webull/core/vendored/requests/sessions.py +750 -0
- webull/core/vendored/requests/status_codes.py +105 -0
- webull/core/vendored/requests/structures.py +119 -0
- webull/core/vendored/requests/utils.py +916 -0
- webull/core/vendored/six.py +905 -0
- webull/data/__init__.py +3 -0
- webull/data/common/__init__.py +0 -0
- webull/data/common/category.py +26 -0
- webull/data/common/connect_ack.py +29 -0
- webull/data/common/direction.py +25 -0
- webull/data/common/exchange_code.py +33 -0
- webull/data/common/exercise_style.py +22 -0
- webull/data/common/expiration_cycle.py +26 -0
- webull/data/common/instrument_status.py +23 -0
- webull/data/common/option_type.py +20 -0
- webull/data/common/subscribe_type.py +22 -0
- webull/data/common/timespan.py +29 -0
- webull/data/data_client.py +35 -0
- webull/data/data_streaming_client.py +89 -0
- webull/data/internal/__init__.py +0 -0
- webull/data/internal/default_retry_policy.py +84 -0
- webull/data/internal/exceptions.py +60 -0
- webull/data/internal/quotes_client.py +314 -0
- webull/data/internal/quotes_decoder.py +40 -0
- webull/data/internal/quotes_payload_decoder.py +35 -0
- webull/data/internal/quotes_topic.py +36 -0
- webull/data/quotes/__init__.py +0 -0
- webull/data/quotes/instrument.py +33 -0
- webull/data/quotes/market_data.py +187 -0
- webull/data/quotes/market_streaming_data.py +66 -0
- webull/data/quotes/subscribe/__init__.py +0 -0
- webull/data/quotes/subscribe/ask_bid_result.py +49 -0
- webull/data/quotes/subscribe/basic_result.py +45 -0
- webull/data/quotes/subscribe/broker_result.py +33 -0
- webull/data/quotes/subscribe/message_pb2.py +37 -0
- webull/data/quotes/subscribe/order_result.py +30 -0
- webull/data/quotes/subscribe/payload_type.py +19 -0
- webull/data/quotes/subscribe/quote_decoder.py +28 -0
- webull/data/quotes/subscribe/quote_result.py +47 -0
- webull/data/quotes/subscribe/snapshot_decoder.py +30 -0
- webull/data/quotes/subscribe/snapshot_result.py +69 -0
- webull/data/quotes/subscribe/tick_decoder.py +29 -0
- webull/data/quotes/subscribe/tick_result.py +47 -0
- webull/data/request/__init__.py +0 -0
- webull/data/request/get_batch_historical_bars_request.py +43 -0
- webull/data/request/get_corp_action_request.py +47 -0
- webull/data/request/get_eod_bars_request.py +32 -0
- webull/data/request/get_historical_bars_request.py +43 -0
- webull/data/request/get_instruments_request.py +30 -0
- webull/data/request/get_quotes_request.py +35 -0
- webull/data/request/get_snapshot_request.py +38 -0
- webull/data/request/get_tick_request.py +37 -0
- webull/data/request/subscribe_request.py +43 -0
- webull/data/request/unsubscribe_request.py +42 -0
- webull/trade/__init__.py +2 -0
- webull/trade/common/__init__.py +0 -0
- webull/trade/common/account_type.py +22 -0
- webull/trade/common/category.py +29 -0
- webull/trade/common/combo_ticker_type.py +23 -0
- webull/trade/common/combo_type.py +31 -0
- webull/trade/common/currency.py +24 -0
- webull/trade/common/forbid_reason.py +27 -0
- webull/trade/common/instrument_type.py +27 -0
- webull/trade/common/markets.py +27 -0
- webull/trade/common/order_entrust_type.py +21 -0
- webull/trade/common/order_side.py +23 -0
- webull/trade/common/order_status.py +25 -0
- webull/trade/common/order_tif.py +24 -0
- webull/trade/common/order_type.py +30 -0
- webull/trade/common/trade_policy.py +22 -0
- webull/trade/common/trading_date_type.py +24 -0
- webull/trade/common/trailing_type.py +23 -0
- webull/trade/events/__init__.py +0 -0
- webull/trade/events/default_retry_policy.py +64 -0
- webull/trade/events/events_pb2.py +43 -0
- webull/trade/events/events_pb2_grpc.py +66 -0
- webull/trade/events/signature_composer.py +61 -0
- webull/trade/events/types.py +21 -0
- webull/trade/request/__init__.py +0 -0
- webull/trade/request/cancel_order_request.py +28 -0
- webull/trade/request/get_account_balance_request.py +28 -0
- webull/trade/request/get_account_positions_request.py +30 -0
- webull/trade/request/get_account_profile_request.py +26 -0
- webull/trade/request/get_app_subscriptions.py +28 -0
- webull/trade/request/get_open_orders_request.py +30 -0
- webull/trade/request/get_order_detail_request.py +27 -0
- webull/trade/request/get_today_orders_request.py +31 -0
- webull/trade/request/get_trade_calendar_request.py +30 -0
- webull/trade/request/get_trade_instrument_detail_request.py +24 -0
- webull/trade/request/get_trade_security_detail_request.py +42 -0
- webull/trade/request/get_tradeable_instruments_request.py +27 -0
- webull/trade/request/palce_order_request.py +91 -0
- webull/trade/request/place_order_request_v2.py +58 -0
- webull/trade/request/replace_order_request.py +73 -0
- webull/trade/request/replace_order_request_v2.py +38 -0
- webull/trade/request/v2/__init__.py +0 -0
- webull/trade/request/v2/cancel_option_request.py +28 -0
- webull/trade/request/v2/cancel_order_request.py +28 -0
- webull/trade/request/v2/get_account_balance_request.py +28 -0
- webull/trade/request/v2/get_account_list.py +23 -0
- webull/trade/request/v2/get_account_positions_request.py +24 -0
- webull/trade/request/v2/get_order_detail_request.py +26 -0
- webull/trade/request/v2/get_order_history_request.py +35 -0
- webull/trade/request/v2/palce_order_request.py +87 -0
- webull/trade/request/v2/place_option_request.py +64 -0
- webull/trade/request/v2/preview_option_request.py +28 -0
- webull/trade/request/v2/preview_order_request.py +59 -0
- webull/trade/request/v2/replace_option_request.py +28 -0
- webull/trade/request/v2/replace_order_request.py +57 -0
- webull/trade/trade/__init__.py +0 -0
- webull/trade/trade/account_info.py +83 -0
- webull/trade/trade/order_operation.py +246 -0
- webull/trade/trade/trade_calendar.py +37 -0
- webull/trade/trade/trade_instrument.py +72 -0
- webull/trade/trade/v2/__init__.py +0 -0
- webull/trade/trade/v2/account_info_v2.py +55 -0
- webull/trade/trade/v2/order_operation_v2.py +206 -0
- webull/trade/trade_client.py +43 -0
- webull/trade/trade_events_client.py +233 -0
- webull_openapi_python_sdk-1.0.0.dist-info/METADATA +28 -0
- webull_openapi_python_sdk-1.0.0.dist-info/RECORD +295 -0
- webull_openapi_python_sdk-1.0.0.dist-info/WHEEL +5 -0
- webull_openapi_python_sdk-1.0.0.dist-info/licenses/LICENSE +202 -0
- webull_openapi_python_sdk-1.0.0.dist-info/licenses/NOTICE +56 -0
- webull_openapi_python_sdk-1.0.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
try:
|
|
4
|
+
# Our match_hostname function is the same as 3.5's, so we only want to
|
|
5
|
+
# import the match_hostname function if it's at least that good.
|
|
6
|
+
if sys.version_info < (3, 5):
|
|
7
|
+
raise ImportError("Fallback to vendored code")
|
|
8
|
+
|
|
9
|
+
from ssl import CertificateError, match_hostname
|
|
10
|
+
except ImportError:
|
|
11
|
+
try:
|
|
12
|
+
# Backport of the function from a pypi module
|
|
13
|
+
from backports.ssl_match_hostname import CertificateError, match_hostname
|
|
14
|
+
except ImportError:
|
|
15
|
+
# Our vendored copy
|
|
16
|
+
from ._implementation import CertificateError, match_hostname
|
|
17
|
+
|
|
18
|
+
# Not needed, but documenting what we provide.
|
|
19
|
+
__all__ = ('CertificateError', 'match_hostname')
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# Copyright 2022 Webull
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""The match_hostname() function from Python 3.3.3, essential when using SSL."""
|
|
16
|
+
|
|
17
|
+
# Note: This file is under the PSF license as the code comes from the python
|
|
18
|
+
# stdlib. http://docs.python.org/3/license.html
|
|
19
|
+
|
|
20
|
+
import re
|
|
21
|
+
import sys
|
|
22
|
+
|
|
23
|
+
# ipaddress has been backported to 2.6+ in pypi. If it is installed on the
|
|
24
|
+
# system, use it to handle IPAddress ServerAltnames (this was added in
|
|
25
|
+
# python-3.5) otherwise only do DNS matching. This allows
|
|
26
|
+
# backports.ssl_match_hostname to continue to be used in Python 2.7.
|
|
27
|
+
try:
|
|
28
|
+
import ipaddress
|
|
29
|
+
except ImportError:
|
|
30
|
+
ipaddress = None
|
|
31
|
+
|
|
32
|
+
__version__ = '3.5.0.1'
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class CertificateError(ValueError):
|
|
36
|
+
pass
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _dnsname_match(dn, hostname, max_wildcards=1):
|
|
40
|
+
"""Matching according to RFC 6125, section 6.4.3
|
|
41
|
+
|
|
42
|
+
http://tools.ietf.org/html/rfc6125#section-6.4.3
|
|
43
|
+
"""
|
|
44
|
+
pats = []
|
|
45
|
+
if not dn:
|
|
46
|
+
return False
|
|
47
|
+
|
|
48
|
+
# Ported from python3-syntax:
|
|
49
|
+
# leftmost, *remainder = dn.split(r'.')
|
|
50
|
+
parts = dn.split(r'.')
|
|
51
|
+
leftmost = parts[0]
|
|
52
|
+
remainder = parts[1:]
|
|
53
|
+
|
|
54
|
+
wildcards = leftmost.count('*')
|
|
55
|
+
if wildcards > max_wildcards:
|
|
56
|
+
# Issue #17980: avoid denials of service by refusing more
|
|
57
|
+
# than one wildcard per fragment. A survey of established
|
|
58
|
+
# policy among SSL implementations showed it to be a
|
|
59
|
+
# reasonable choice.
|
|
60
|
+
raise CertificateError(
|
|
61
|
+
"too many wildcards in certificate DNS name: " + repr(dn))
|
|
62
|
+
|
|
63
|
+
# speed up common case w/o wildcards
|
|
64
|
+
if not wildcards:
|
|
65
|
+
return dn.lower() == hostname.lower()
|
|
66
|
+
|
|
67
|
+
# RFC 6125, section 6.4.3, subitem 1.
|
|
68
|
+
# The client SHOULD NOT attempt to match a presented identifier in which
|
|
69
|
+
# the wildcard character comprises a label other than the left-most label.
|
|
70
|
+
if leftmost == '*':
|
|
71
|
+
# When '*' is a fragment by itself, it matches a non-empty dotless
|
|
72
|
+
# fragment.
|
|
73
|
+
pats.append('[^.]+')
|
|
74
|
+
elif leftmost.startswith('xn--') or hostname.startswith('xn--'):
|
|
75
|
+
# RFC 6125, section 6.4.3, subitem 3.
|
|
76
|
+
# The client SHOULD NOT attempt to match a presented identifier
|
|
77
|
+
# where the wildcard character is embedded within an A-label or
|
|
78
|
+
# U-label of an internationalized domain name.
|
|
79
|
+
pats.append(re.escape(leftmost))
|
|
80
|
+
else:
|
|
81
|
+
# Otherwise, '*' matches any dotless string, e.g. www*
|
|
82
|
+
pats.append(re.escape(leftmost).replace(r'\*', '[^.]*'))
|
|
83
|
+
|
|
84
|
+
# add the remaining fragments, ignore any wildcards
|
|
85
|
+
for frag in remainder:
|
|
86
|
+
pats.append(re.escape(frag))
|
|
87
|
+
|
|
88
|
+
pat = re.compile(r'\A' + r'\.'.join(pats) + r'\Z', re.IGNORECASE)
|
|
89
|
+
return pat.match(hostname)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _to_unicode(obj):
|
|
93
|
+
if isinstance(obj, str) and sys.version_info < (3,):
|
|
94
|
+
obj = unicode(obj, encoding='ascii', errors='strict')
|
|
95
|
+
return obj
|
|
96
|
+
|
|
97
|
+
def _ipaddress_match(ipname, host_ip):
|
|
98
|
+
"""Exact matching of IP addresses.
|
|
99
|
+
|
|
100
|
+
RFC 6125 explicitly doesn't define an algorithm for this
|
|
101
|
+
(section 1.7.2 - "Out of Scope").
|
|
102
|
+
"""
|
|
103
|
+
# OpenSSL may add a trailing newline to a subjectAltName's IP address
|
|
104
|
+
# Divergence from upstream: ipaddress can't handle byte str
|
|
105
|
+
ip = ipaddress.ip_address(_to_unicode(ipname).rstrip())
|
|
106
|
+
return ip == host_ip
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def match_hostname(cert, hostname):
|
|
110
|
+
"""Verify that *cert* (in decoded format as returned by
|
|
111
|
+
SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125
|
|
112
|
+
rules are followed, but IP addresses are not accepted for *hostname*.
|
|
113
|
+
|
|
114
|
+
CertificateError is raised on failure. On success, the function
|
|
115
|
+
returns nothing.
|
|
116
|
+
"""
|
|
117
|
+
if not cert:
|
|
118
|
+
raise ValueError("empty or no certificate, match_hostname needs a "
|
|
119
|
+
"SSL socket or SSL context with either "
|
|
120
|
+
"CERT_OPTIONAL or CERT_REQUIRED")
|
|
121
|
+
try:
|
|
122
|
+
# Divergence from upstream: ipaddress can't handle byte str
|
|
123
|
+
host_ip = ipaddress.ip_address(_to_unicode(hostname))
|
|
124
|
+
except UnicodeError:
|
|
125
|
+
# Divergence from upstream: Have to deal with ipaddress not taking
|
|
126
|
+
# byte strings. addresses should be all ascii, so we consider it not
|
|
127
|
+
# an ipaddress in this case
|
|
128
|
+
host_ip = None
|
|
129
|
+
except ValueError:
|
|
130
|
+
# Not an IP address (common case)
|
|
131
|
+
host_ip = None
|
|
132
|
+
except AttributeError:
|
|
133
|
+
# Divergence from upstream: Make ipaddress library optional
|
|
134
|
+
if ipaddress is None:
|
|
135
|
+
host_ip = None
|
|
136
|
+
else:
|
|
137
|
+
raise
|
|
138
|
+
dnsnames = []
|
|
139
|
+
san = cert.get('subjectAltName', ())
|
|
140
|
+
for key, value in san:
|
|
141
|
+
if key == 'DNS':
|
|
142
|
+
if host_ip is None and _dnsname_match(value, hostname):
|
|
143
|
+
return
|
|
144
|
+
dnsnames.append(value)
|
|
145
|
+
elif key == 'IP Address':
|
|
146
|
+
if host_ip is not None and _ipaddress_match(value, host_ip):
|
|
147
|
+
return
|
|
148
|
+
dnsnames.append(value)
|
|
149
|
+
if not dnsnames:
|
|
150
|
+
# The subject is only checked when there is no dNSName entry
|
|
151
|
+
# in subjectAltName
|
|
152
|
+
for sub in cert.get('subject', ()):
|
|
153
|
+
for key, value in sub:
|
|
154
|
+
# XXX according to RFC 2818, the most specific Common Name
|
|
155
|
+
# must be used.
|
|
156
|
+
if key == 'commonName':
|
|
157
|
+
if _dnsname_match(value, hostname):
|
|
158
|
+
return
|
|
159
|
+
dnsnames.append(value)
|
|
160
|
+
if len(dnsnames) > 1:
|
|
161
|
+
raise CertificateError("hostname %r "
|
|
162
|
+
"doesn't match either of %s"
|
|
163
|
+
% (hostname, ', '.join(map(repr, dnsnames))))
|
|
164
|
+
elif len(dnsnames) == 1:
|
|
165
|
+
raise CertificateError("hostname %r "
|
|
166
|
+
"doesn't match %r"
|
|
167
|
+
% (hostname, dnsnames[0]))
|
|
168
|
+
else:
|
|
169
|
+
raise CertificateError("no appropriate commonName or "
|
|
170
|
+
"subjectAltName fields were found")
|
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
# Copyright 2022 Webull
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from __future__ import absolute_import
|
|
16
|
+
import collections
|
|
17
|
+
import functools
|
|
18
|
+
import logging
|
|
19
|
+
|
|
20
|
+
from ._collections import RecentlyUsedContainer
|
|
21
|
+
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool
|
|
22
|
+
from .connectionpool import port_by_scheme
|
|
23
|
+
from .exceptions import LocationValueError, MaxRetryError, ProxySchemeUnknown
|
|
24
|
+
from .packages import six
|
|
25
|
+
from .packages.six.moves.urllib.parse import urljoin
|
|
26
|
+
from .request import RequestMethods
|
|
27
|
+
from .util.url import parse_url
|
|
28
|
+
from .util.retry import Retry
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
__all__ = ['PoolManager', 'ProxyManager', 'proxy_from_url']
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
log = logging.getLogger(__name__)
|
|
35
|
+
|
|
36
|
+
SSL_KEYWORDS = ('key_file', 'cert_file', 'cert_reqs', 'ca_certs',
|
|
37
|
+
'ssl_version', 'ca_cert_dir', 'ssl_context')
|
|
38
|
+
|
|
39
|
+
# All known keyword arguments that could be provided to the pool manager, its
|
|
40
|
+
# pools, or the underlying connections. This is used to construct a pool key.
|
|
41
|
+
_key_fields = (
|
|
42
|
+
'key_scheme', # str
|
|
43
|
+
'key_host', # str
|
|
44
|
+
'key_port', # int
|
|
45
|
+
'key_timeout', # int or float or Timeout
|
|
46
|
+
'key_retries', # int or Retry
|
|
47
|
+
'key_strict', # bool
|
|
48
|
+
'key_block', # bool
|
|
49
|
+
'key_source_address', # str
|
|
50
|
+
'key_key_file', # str
|
|
51
|
+
'key_cert_file', # str
|
|
52
|
+
'key_cert_reqs', # str
|
|
53
|
+
'key_ca_certs', # str
|
|
54
|
+
'key_ssl_version', # str
|
|
55
|
+
'key_ca_cert_dir', # str
|
|
56
|
+
'key_ssl_context', # instance of ssl.SSLContext or urllib3.util.ssl_.SSLContext
|
|
57
|
+
'key_maxsize', # int
|
|
58
|
+
'key_headers', # dict
|
|
59
|
+
'key__proxy', # parsed proxy url
|
|
60
|
+
'key__proxy_headers', # dict
|
|
61
|
+
'key_socket_options', # list of (level (int), optname (int), value (int or str)) tuples
|
|
62
|
+
'key__socks_options', # dict
|
|
63
|
+
'key_assert_hostname', # bool or string
|
|
64
|
+
'key_assert_fingerprint', # str
|
|
65
|
+
'key_server_hostname', # str
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
#: The namedtuple class used to construct keys for the connection pool.
|
|
69
|
+
#: All custom key schemes should include the fields in this key at a minimum.
|
|
70
|
+
PoolKey = collections.namedtuple('PoolKey', _key_fields)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _default_key_normalizer(key_class, request_context):
|
|
74
|
+
"""
|
|
75
|
+
Create a pool key out of a request context dictionary.
|
|
76
|
+
|
|
77
|
+
According to RFC 3986, both the scheme and host are case-insensitive.
|
|
78
|
+
Therefore, this function normalizes both before constructing the pool
|
|
79
|
+
key for an HTTPS request. If you wish to change this behaviour, provide
|
|
80
|
+
alternate callables to ``key_fn_by_scheme``.
|
|
81
|
+
|
|
82
|
+
:param key_class:
|
|
83
|
+
The class to use when constructing the key. This should be a namedtuple
|
|
84
|
+
with the ``scheme`` and ``host`` keys at a minimum.
|
|
85
|
+
:type key_class: namedtuple
|
|
86
|
+
:param request_context:
|
|
87
|
+
A dictionary-like object that contain the context for a request.
|
|
88
|
+
:type request_context: dict
|
|
89
|
+
|
|
90
|
+
:return: A namedtuple that can be used as a connection pool key.
|
|
91
|
+
:rtype: PoolKey
|
|
92
|
+
"""
|
|
93
|
+
# Since we mutate the dictionary, make a copy first
|
|
94
|
+
context = request_context.copy()
|
|
95
|
+
context['scheme'] = context['scheme'].lower()
|
|
96
|
+
context['host'] = context['host'].lower()
|
|
97
|
+
|
|
98
|
+
# These are both dictionaries and need to be transformed into frozensets
|
|
99
|
+
for key in ('headers', '_proxy_headers', '_socks_options'):
|
|
100
|
+
if key in context and context[key] is not None:
|
|
101
|
+
context[key] = frozenset(context[key].items())
|
|
102
|
+
|
|
103
|
+
# The socket_options key may be a list and needs to be transformed into a
|
|
104
|
+
# tuple.
|
|
105
|
+
socket_opts = context.get('socket_options')
|
|
106
|
+
if socket_opts is not None:
|
|
107
|
+
context['socket_options'] = tuple(socket_opts)
|
|
108
|
+
|
|
109
|
+
# Map the kwargs to the names in the namedtuple - this is necessary since
|
|
110
|
+
# namedtuples can't have fields starting with '_'.
|
|
111
|
+
for key in list(context.keys()):
|
|
112
|
+
context['key_' + key] = context.pop(key)
|
|
113
|
+
|
|
114
|
+
# Default to ``None`` for keys missing from the context
|
|
115
|
+
for field in key_class._fields:
|
|
116
|
+
if field not in context:
|
|
117
|
+
context[field] = None
|
|
118
|
+
|
|
119
|
+
return key_class(**context)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
#: A dictionary that maps a scheme to a callable that creates a pool key.
|
|
123
|
+
#: This can be used to alter the way pool keys are constructed, if desired.
|
|
124
|
+
#: Each PoolManager makes a copy of this dictionary so they can be configured
|
|
125
|
+
#: globally here, or individually on the instance.
|
|
126
|
+
key_fn_by_scheme = {
|
|
127
|
+
'http': functools.partial(_default_key_normalizer, PoolKey),
|
|
128
|
+
'https': functools.partial(_default_key_normalizer, PoolKey),
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
pool_classes_by_scheme = {
|
|
132
|
+
'http': HTTPConnectionPool,
|
|
133
|
+
'https': HTTPSConnectionPool,
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class PoolManager(RequestMethods):
|
|
138
|
+
"""
|
|
139
|
+
Allows for arbitrary requests while transparently keeping track of
|
|
140
|
+
necessary connection pools for you.
|
|
141
|
+
|
|
142
|
+
:param num_pools:
|
|
143
|
+
Number of connection pools to cache before discarding the least
|
|
144
|
+
recently used pool.
|
|
145
|
+
|
|
146
|
+
:param headers:
|
|
147
|
+
Headers to include with all requests, unless other headers are given
|
|
148
|
+
explicitly.
|
|
149
|
+
|
|
150
|
+
:param \\**connection_pool_kw:
|
|
151
|
+
Additional parameters are used to create fresh
|
|
152
|
+
:class:`urllib3.connectionpool.ConnectionPool` instances.
|
|
153
|
+
|
|
154
|
+
Example::
|
|
155
|
+
|
|
156
|
+
>>> manager = PoolManager(num_pools=2)
|
|
157
|
+
>>> r = manager.request('GET', 'http://google.com/')
|
|
158
|
+
>>> r = manager.request('GET', 'http://google.com/mail')
|
|
159
|
+
>>> r = manager.request('GET', 'http://yahoo.com/')
|
|
160
|
+
>>> len(manager.pools)
|
|
161
|
+
2
|
|
162
|
+
|
|
163
|
+
"""
|
|
164
|
+
|
|
165
|
+
proxy = None
|
|
166
|
+
|
|
167
|
+
def __init__(self, num_pools=10, headers=None, **connection_pool_kw):
|
|
168
|
+
RequestMethods.__init__(self, headers)
|
|
169
|
+
self.connection_pool_kw = connection_pool_kw
|
|
170
|
+
self.pools = RecentlyUsedContainer(num_pools,
|
|
171
|
+
dispose_func=lambda p: p.close())
|
|
172
|
+
|
|
173
|
+
# Locally set the pool classes and keys so other PoolManagers can
|
|
174
|
+
# override them.
|
|
175
|
+
self.pool_classes_by_scheme = pool_classes_by_scheme
|
|
176
|
+
self.key_fn_by_scheme = key_fn_by_scheme.copy()
|
|
177
|
+
|
|
178
|
+
def __enter__(self):
|
|
179
|
+
return self
|
|
180
|
+
|
|
181
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
182
|
+
self.clear()
|
|
183
|
+
# Return False to re-raise any potential exceptions
|
|
184
|
+
return False
|
|
185
|
+
|
|
186
|
+
def _new_pool(self, scheme, host, port, request_context=None):
|
|
187
|
+
"""
|
|
188
|
+
Create a new :class:`ConnectionPool` based on host, port, scheme, and
|
|
189
|
+
any additional pool keyword arguments.
|
|
190
|
+
|
|
191
|
+
If ``request_context`` is provided, it is provided as keyword arguments
|
|
192
|
+
to the pool class used. This method is used to actually create the
|
|
193
|
+
connection pools handed out by :meth:`connection_from_url` and
|
|
194
|
+
companion methods. It is intended to be overridden for customization.
|
|
195
|
+
"""
|
|
196
|
+
pool_cls = self.pool_classes_by_scheme[scheme]
|
|
197
|
+
if request_context is None:
|
|
198
|
+
request_context = self.connection_pool_kw.copy()
|
|
199
|
+
|
|
200
|
+
# Although the context has everything necessary to create the pool,
|
|
201
|
+
# this function has historically only used the scheme, host, and port
|
|
202
|
+
# in the positional args. When an API change is acceptable these can
|
|
203
|
+
# be removed.
|
|
204
|
+
for key in ('scheme', 'host', 'port'):
|
|
205
|
+
request_context.pop(key, None)
|
|
206
|
+
|
|
207
|
+
if scheme == 'http':
|
|
208
|
+
for kw in SSL_KEYWORDS:
|
|
209
|
+
request_context.pop(kw, None)
|
|
210
|
+
|
|
211
|
+
return pool_cls(host, port, **request_context)
|
|
212
|
+
|
|
213
|
+
def clear(self):
|
|
214
|
+
"""
|
|
215
|
+
Empty our store of pools and direct them all to close.
|
|
216
|
+
|
|
217
|
+
This will not affect in-flight connections, but they will not be
|
|
218
|
+
re-used after completion.
|
|
219
|
+
"""
|
|
220
|
+
self.pools.clear()
|
|
221
|
+
|
|
222
|
+
def connection_from_host(self, host, port=None, scheme='http', pool_kwargs=None):
|
|
223
|
+
"""
|
|
224
|
+
Get a :class:`ConnectionPool` based on the host, port, and scheme.
|
|
225
|
+
|
|
226
|
+
If ``port`` isn't given, it will be derived from the ``scheme`` using
|
|
227
|
+
``urllib3.connectionpool.port_by_scheme``. If ``pool_kwargs`` is
|
|
228
|
+
provided, it is merged with the instance's ``connection_pool_kw``
|
|
229
|
+
variable and used to create the new connection pool, if one is
|
|
230
|
+
needed.
|
|
231
|
+
"""
|
|
232
|
+
|
|
233
|
+
if not host:
|
|
234
|
+
raise LocationValueError("No host specified.")
|
|
235
|
+
|
|
236
|
+
request_context = self._merge_pool_kwargs(pool_kwargs)
|
|
237
|
+
request_context['scheme'] = scheme or 'http'
|
|
238
|
+
if not port:
|
|
239
|
+
port = port_by_scheme.get(request_context['scheme'].lower(), 80)
|
|
240
|
+
request_context['port'] = port
|
|
241
|
+
request_context['host'] = host
|
|
242
|
+
|
|
243
|
+
return self.connection_from_context(request_context)
|
|
244
|
+
|
|
245
|
+
def connection_from_context(self, request_context):
|
|
246
|
+
"""
|
|
247
|
+
Get a :class:`ConnectionPool` based on the request context.
|
|
248
|
+
|
|
249
|
+
``request_context`` must at least contain the ``scheme`` key and its
|
|
250
|
+
value must be a key in ``key_fn_by_scheme`` instance variable.
|
|
251
|
+
"""
|
|
252
|
+
scheme = request_context['scheme'].lower()
|
|
253
|
+
pool_key_constructor = self.key_fn_by_scheme[scheme]
|
|
254
|
+
pool_key = pool_key_constructor(request_context)
|
|
255
|
+
|
|
256
|
+
return self.connection_from_pool_key(pool_key, request_context=request_context)
|
|
257
|
+
|
|
258
|
+
def connection_from_pool_key(self, pool_key, request_context=None):
|
|
259
|
+
"""
|
|
260
|
+
Get a :class:`ConnectionPool` based on the provided pool key.
|
|
261
|
+
|
|
262
|
+
``pool_key`` should be a namedtuple that only contains immutable
|
|
263
|
+
objects. At a minimum it must have the ``scheme``, ``host``, and
|
|
264
|
+
``port`` fields.
|
|
265
|
+
"""
|
|
266
|
+
with self.pools.lock:
|
|
267
|
+
# If the scheme, host, or port doesn't match existing open
|
|
268
|
+
# connections, open a new ConnectionPool.
|
|
269
|
+
pool = self.pools.get(pool_key)
|
|
270
|
+
if pool:
|
|
271
|
+
return pool
|
|
272
|
+
|
|
273
|
+
# Make a fresh ConnectionPool of the desired type
|
|
274
|
+
scheme = request_context['scheme']
|
|
275
|
+
host = request_context['host']
|
|
276
|
+
port = request_context['port']
|
|
277
|
+
pool = self._new_pool(scheme, host, port, request_context=request_context)
|
|
278
|
+
self.pools[pool_key] = pool
|
|
279
|
+
|
|
280
|
+
return pool
|
|
281
|
+
|
|
282
|
+
def connection_from_url(self, url, pool_kwargs=None):
|
|
283
|
+
"""
|
|
284
|
+
Similar to :func:`urllib3.connectionpool.connection_from_url`.
|
|
285
|
+
|
|
286
|
+
If ``pool_kwargs`` is not provided and a new pool needs to be
|
|
287
|
+
constructed, ``self.connection_pool_kw`` is used to initialize
|
|
288
|
+
the :class:`urllib3.connectionpool.ConnectionPool`. If ``pool_kwargs``
|
|
289
|
+
is provided, it is used instead. Note that if a new pool does not
|
|
290
|
+
need to be created for the request, the provided ``pool_kwargs`` are
|
|
291
|
+
not used.
|
|
292
|
+
"""
|
|
293
|
+
u = parse_url(url)
|
|
294
|
+
return self.connection_from_host(u.host, port=u.port, scheme=u.scheme,
|
|
295
|
+
pool_kwargs=pool_kwargs)
|
|
296
|
+
|
|
297
|
+
def _merge_pool_kwargs(self, override):
|
|
298
|
+
"""
|
|
299
|
+
Merge a dictionary of override values for self.connection_pool_kw.
|
|
300
|
+
|
|
301
|
+
This does not modify self.connection_pool_kw and returns a new dict.
|
|
302
|
+
Any keys in the override dictionary with a value of ``None`` are
|
|
303
|
+
removed from the merged dictionary.
|
|
304
|
+
"""
|
|
305
|
+
base_pool_kwargs = self.connection_pool_kw.copy()
|
|
306
|
+
if override:
|
|
307
|
+
for key, value in override.items():
|
|
308
|
+
if value is None:
|
|
309
|
+
try:
|
|
310
|
+
del base_pool_kwargs[key]
|
|
311
|
+
except KeyError:
|
|
312
|
+
pass
|
|
313
|
+
else:
|
|
314
|
+
base_pool_kwargs[key] = value
|
|
315
|
+
return base_pool_kwargs
|
|
316
|
+
|
|
317
|
+
def urlopen(self, method, url, redirect=True, **kw):
|
|
318
|
+
"""
|
|
319
|
+
Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen`
|
|
320
|
+
with custom cross-host redirect logic and only sends the request-uri
|
|
321
|
+
portion of the ``url``.
|
|
322
|
+
|
|
323
|
+
The given ``url`` parameter must be absolute, such that an appropriate
|
|
324
|
+
:class:`urllib3.connectionpool.ConnectionPool` can be chosen for it.
|
|
325
|
+
"""
|
|
326
|
+
u = parse_url(url)
|
|
327
|
+
conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
|
|
328
|
+
|
|
329
|
+
kw['assert_same_host'] = False
|
|
330
|
+
kw['redirect'] = False
|
|
331
|
+
|
|
332
|
+
if 'headers' not in kw:
|
|
333
|
+
kw['headers'] = self.headers.copy()
|
|
334
|
+
|
|
335
|
+
if self.proxy is not None and u.scheme == "http":
|
|
336
|
+
response = conn.urlopen(method, url, **kw)
|
|
337
|
+
else:
|
|
338
|
+
response = conn.urlopen(method, u.request_uri, **kw)
|
|
339
|
+
|
|
340
|
+
redirect_location = redirect and response.get_redirect_location()
|
|
341
|
+
if not redirect_location:
|
|
342
|
+
return response
|
|
343
|
+
|
|
344
|
+
# Support relative URLs for redirecting.
|
|
345
|
+
redirect_location = urljoin(url, redirect_location)
|
|
346
|
+
|
|
347
|
+
# RFC 7231, Section 6.4.4
|
|
348
|
+
if response.status == 303:
|
|
349
|
+
method = 'GET'
|
|
350
|
+
|
|
351
|
+
retries = kw.get('retries')
|
|
352
|
+
if not isinstance(retries, Retry):
|
|
353
|
+
retries = Retry.from_int(retries, redirect=redirect)
|
|
354
|
+
|
|
355
|
+
# Strip headers marked as unsafe to forward to the redirected location.
|
|
356
|
+
# Check remove_headers_on_redirect to avoid a potential network call within
|
|
357
|
+
# conn.is_same_host() which may use socket.gethostbyname() in the future.
|
|
358
|
+
if (retries.remove_headers_on_redirect
|
|
359
|
+
and not conn.is_same_host(redirect_location)):
|
|
360
|
+
headers = list(six.iterkeys(kw['headers']))
|
|
361
|
+
for header in headers:
|
|
362
|
+
if header.lower() in retries.remove_headers_on_redirect:
|
|
363
|
+
kw['headers'].pop(header, None)
|
|
364
|
+
|
|
365
|
+
try:
|
|
366
|
+
retries = retries.increment(method, url, response=response, _pool=conn)
|
|
367
|
+
except MaxRetryError:
|
|
368
|
+
if retries.raise_on_redirect:
|
|
369
|
+
raise
|
|
370
|
+
return response
|
|
371
|
+
|
|
372
|
+
kw['retries'] = retries
|
|
373
|
+
kw['redirect'] = redirect
|
|
374
|
+
|
|
375
|
+
log.info("Redirecting %s -> %s", url, redirect_location)
|
|
376
|
+
return self.urlopen(method, redirect_location, **kw)
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
class ProxyManager(PoolManager):
|
|
380
|
+
"""
|
|
381
|
+
Behaves just like :class:`PoolManager`, but sends all requests through
|
|
382
|
+
the defined proxy, using the CONNECT method for HTTPS URLs.
|
|
383
|
+
|
|
384
|
+
:param proxy_url:
|
|
385
|
+
The URL of the proxy to be used.
|
|
386
|
+
|
|
387
|
+
:param proxy_headers:
|
|
388
|
+
A dictionary containing headers that will be sent to the proxy. In case
|
|
389
|
+
of HTTP they are being sent with each request, while in the
|
|
390
|
+
HTTPS/CONNECT case they are sent only once. Could be used for proxy
|
|
391
|
+
authentication.
|
|
392
|
+
|
|
393
|
+
Example:
|
|
394
|
+
>>> proxy = urllib3.ProxyManager('http://localhost:3128/')
|
|
395
|
+
>>> r1 = proxy.request('GET', 'http://google.com/')
|
|
396
|
+
>>> r2 = proxy.request('GET', 'http://httpbin.org/')
|
|
397
|
+
>>> len(proxy.pools)
|
|
398
|
+
1
|
|
399
|
+
>>> r3 = proxy.request('GET', 'https://httpbin.org/')
|
|
400
|
+
>>> r4 = proxy.request('GET', 'https://twitter.com/')
|
|
401
|
+
>>> len(proxy.pools)
|
|
402
|
+
3
|
|
403
|
+
|
|
404
|
+
"""
|
|
405
|
+
|
|
406
|
+
def __init__(self, proxy_url, num_pools=10, headers=None,
|
|
407
|
+
proxy_headers=None, **connection_pool_kw):
|
|
408
|
+
|
|
409
|
+
if isinstance(proxy_url, HTTPConnectionPool):
|
|
410
|
+
proxy_url = '%s://%s:%i' % (proxy_url.scheme, proxy_url.host,
|
|
411
|
+
proxy_url.port)
|
|
412
|
+
proxy = parse_url(proxy_url)
|
|
413
|
+
if not proxy.port:
|
|
414
|
+
port = port_by_scheme.get(proxy.scheme, 80)
|
|
415
|
+
proxy = proxy._replace(port=port)
|
|
416
|
+
|
|
417
|
+
if proxy.scheme not in ("http", "https"):
|
|
418
|
+
raise ProxySchemeUnknown(proxy.scheme)
|
|
419
|
+
|
|
420
|
+
self.proxy = proxy
|
|
421
|
+
self.proxy_headers = proxy_headers or {}
|
|
422
|
+
|
|
423
|
+
connection_pool_kw['_proxy'] = self.proxy
|
|
424
|
+
connection_pool_kw['_proxy_headers'] = self.proxy_headers
|
|
425
|
+
|
|
426
|
+
super(ProxyManager, self).__init__(
|
|
427
|
+
num_pools, headers, **connection_pool_kw)
|
|
428
|
+
|
|
429
|
+
def connection_from_host(self, host, port=None, scheme='http', pool_kwargs=None):
|
|
430
|
+
if scheme == "https":
|
|
431
|
+
return super(ProxyManager, self).connection_from_host(
|
|
432
|
+
host, port, scheme, pool_kwargs=pool_kwargs)
|
|
433
|
+
|
|
434
|
+
return super(ProxyManager, self).connection_from_host(
|
|
435
|
+
self.proxy.host, self.proxy.port, self.proxy.scheme, pool_kwargs=pool_kwargs)
|
|
436
|
+
|
|
437
|
+
def _set_proxy_headers(self, url, headers=None):
|
|
438
|
+
"""
|
|
439
|
+
Sets headers needed by proxies: specifically, the Accept and Host
|
|
440
|
+
headers. Only sets headers not provided by the user.
|
|
441
|
+
"""
|
|
442
|
+
headers_ = {'Accept': '*/*'}
|
|
443
|
+
|
|
444
|
+
netloc = parse_url(url).netloc
|
|
445
|
+
if netloc:
|
|
446
|
+
headers_['Host'] = netloc
|
|
447
|
+
|
|
448
|
+
if headers:
|
|
449
|
+
headers_.update(headers)
|
|
450
|
+
return headers_
|
|
451
|
+
|
|
452
|
+
def urlopen(self, method, url, redirect=True, **kw):
|
|
453
|
+
"Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."
|
|
454
|
+
u = parse_url(url)
|
|
455
|
+
|
|
456
|
+
if u.scheme == "http":
|
|
457
|
+
# For proxied HTTPS requests, httplib sets the necessary headers
|
|
458
|
+
# on the CONNECT to the proxy. For HTTP, we'll definitely
|
|
459
|
+
# need to set 'Host' at the very least.
|
|
460
|
+
headers = kw.get('headers', self.headers)
|
|
461
|
+
kw['headers'] = self._set_proxy_headers(url, headers)
|
|
462
|
+
|
|
463
|
+
return super(ProxyManager, self).urlopen(method, url, redirect=redirect, **kw)
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
def proxy_from_url(url, **kw):
|
|
467
|
+
return ProxyManager(proxy_url=url, **kw)
|