49
49
IPv4Address ,
50
50
IPv6Address )
51
51
52
- try : # pragma: no cover
53
- from urllib .request import (OpenerDirector ,
54
- ProxyHandler ,
55
- build_opener ,
56
- Request ,
57
- URLError ,
58
- HTTPError )
59
- from urllib .parse import urlencode
60
- except ImportError : # pragma: no cover
61
- from urllib2 import (OpenerDirector ,
62
- ProxyHandler ,
63
- build_opener ,
64
- Request ,
65
- URLError ,
66
- HTTPError )
67
- from urllib import urlencode
52
+ from httpx import (Client ,
53
+ HTTPStatusError ,
54
+ TransportError ,
55
+ InvalidURL )
56
+ from urllib .parse import urlencode
68
57
69
58
log = logging .getLogger (__name__ )
70
59
@@ -101,15 +90,15 @@ class Net:
101
90
An IPv4 or IPv6 address
102
91
timeout (:obj:`int`): The default timeout for socket connections in
103
92
seconds. Defaults to 5.
104
- proxy_opener (:obj:`urllib.request.OpenerDirector `): The request for
105
- proxy support. Defaults to None .
93
+ http_client (:obj:`httpx.client `): httpx client allows you to customize
94
+ usage of HTTP by this lib. Proxies are also configured via it .
106
95
107
96
Raises:
108
97
IPDefinedError: The address provided is defined (does not need to be
109
98
resolved).
110
99
"""
111
100
112
- def __init__ (self , address , timeout = 5 , proxy_opener = None ):
101
+ def __init__ (self , address , timeout = 5 , http_client = None ):
113
102
114
103
# IPv4Address or IPv6Address
115
104
if isinstance (address , IPv4Address ) or isinstance (
@@ -129,15 +118,10 @@ def __init__(self, address, timeout=5, proxy_opener=None):
129
118
self .dns_resolver .timeout = timeout
130
119
self .dns_resolver .lifetime = timeout
131
120
132
- # Proxy opener.
133
- if isinstance ( proxy_opener , OpenerDirector ):
121
+ if not http_client :
122
+ http_client = Client ()
134
123
135
- self .opener = proxy_opener
136
-
137
- else :
138
-
139
- handler = ProxyHandler ()
140
- self .opener = build_opener (handler )
124
+ self .http_client = http_client
141
125
142
126
# IP address in string format for use in queries.
143
127
self .address_str = self .address .__str__ ()
@@ -709,10 +693,10 @@ def get_http_json(self, url=None, retry_count=3, rate_limit_timeout=120,
709
693
710
694
return d
711
695
712
- except HTTPError as e : # pragma: no cover
696
+ except HTTPStatusError as e : # pragma: no cover
713
697
714
698
# RIPE is producing this HTTP error rather than a JSON error.
715
- if e .code == 429 :
699
+ if e .response . status_code == 429 :
716
700
717
701
log .debug ('HTTP query rate limit exceeded.' )
718
702
@@ -737,7 +721,7 @@ def get_http_json(self, url=None, retry_count=3, rate_limit_timeout=120,
737
721
raise HTTPLookupError ('HTTP lookup failed for {0} with error '
738
722
'code {1}.' .format (url , str (e .code )))
739
723
740
- except (URLError , socket . timeout , socket . error ) as e :
724
+ except (TransportError , ) as e :
741
725
742
726
log .debug ('HTTP query socket error: {0}' .format (e ))
743
727
if retry_count > 0 :
@@ -865,22 +849,10 @@ def get_http_raw(self, url=None, retry_count=3, headers=None,
865
849
# Create the connection for the HTTP query.
866
850
log .debug ('HTTP query for {0} at {1}' .format (
867
851
self .address_str , url ))
868
- try :
869
- # Py 2 inspection alert bypassed by using kwargs dict.
870
- conn = Request (url = url , data = enc_form_data , headers = headers ,
871
- ** {'method' : request_type })
872
- except TypeError : # pragma: no cover
873
- conn = Request (url = url , data = enc_form_data , headers = headers )
874
- data = self .opener .open (conn , timeout = self .timeout )
875
-
876
- try :
877
- d = data .readall ().decode ('ascii' , 'ignore' )
878
- except AttributeError : # pragma: no cover
879
- d = data .read ().decode ('ascii' , 'ignore' )
880
-
881
- return str (d )
852
+ return self .http_client .request (url = url , data = enc_form_data , headers = headers ,
853
+ ** {'method' : request_type }).text
882
854
883
- except (URLError , socket . timeout , socket . error ) as e :
855
+ except (InvalidURL , TransportError ) as e :
884
856
885
857
log .debug ('HTTP query socket error: {0}' .format (e ))
886
858
if retry_count > 0 :
0 commit comments