33
33
- uses standard Python json lib
34
34
"""
35
35
36
- try :
37
- import http .client as httplib
38
- except ImportError :
39
- import httplib
40
36
import base64
41
37
import decimal
38
+ import http .client
42
39
import json
43
40
import logging
44
41
import socket
45
42
import time
46
- try :
47
- import urllib .parse as urlparse
48
- except ImportError :
49
- import urlparse
50
-
51
- USER_AGENT = "AuthServiceProxy/0.1"
43
+ import urllib .parse
52
44
53
45
HTTP_TIMEOUT = 30
46
+ USER_AGENT = "AuthServiceProxy/0.1"
54
47
55
48
log = logging .getLogger ("BitcoinRPC" )
56
49
@@ -60,7 +53,7 @@ def __init__(self, rpc_error):
60
53
errmsg = '%(message)s (%(code)i)' % rpc_error
61
54
except (KeyError , TypeError ):
62
55
errmsg = ''
63
- Exception .__init__ (self , errmsg )
56
+ super () .__init__ (errmsg )
64
57
self .error = rpc_error
65
58
66
59
@@ -69,40 +62,28 @@ def EncodeDecimal(o):
69
62
return str (o )
70
63
raise TypeError (repr (o ) + " is not JSON serializable" )
71
64
72
- class AuthServiceProxy (object ):
65
+ class AuthServiceProxy ():
73
66
__id_count = 0
74
67
75
68
# ensure_ascii: escape unicode as \uXXXX, passed to json.dumps
76
69
def __init__ (self , service_url , service_name = None , timeout = HTTP_TIMEOUT , connection = None , ensure_ascii = True ):
77
70
self .__service_url = service_url
78
71
self ._service_name = service_name
79
- self .ensure_ascii = ensure_ascii # can be toggled on the fly by tests
80
- self .__url = urlparse .urlparse (service_url )
81
- if self .__url .port is None :
82
- port = 80
83
- else :
84
- port = self .__url .port
85
- (user , passwd ) = (self .__url .username , self .__url .password )
86
- try :
87
- user = user .encode ('utf8' )
88
- except AttributeError :
89
- pass
90
- try :
91
- passwd = passwd .encode ('utf8' )
92
- except AttributeError :
93
- pass
72
+ self .ensure_ascii = ensure_ascii # can be toggled on the fly by tests
73
+ self .__url = urllib .parse .urlparse (service_url )
74
+ port = 80 if self .__url .port is None else self .__url .port
75
+ user = None if self .__url .username is None else self .__url .username .encode ('utf8' )
76
+ passwd = None if self .__url .password is None else self .__url .password .encode ('utf8' )
94
77
authpair = user + b':' + passwd
95
78
self .__auth_header = b'Basic ' + base64 .b64encode (authpair )
96
79
97
80
if connection :
98
81
# Callables re-use the connection of the original proxy
99
82
self .__conn = connection
100
83
elif self .__url .scheme == 'https' :
101
- self .__conn = httplib .HTTPSConnection (self .__url .hostname , port ,
102
- timeout = timeout )
84
+ self .__conn = http .client .HTTPSConnection (self .__url .hostname , port , timeout = timeout )
103
85
else :
104
- self .__conn = httplib .HTTPConnection (self .__url .hostname , port ,
105
- timeout = timeout )
86
+ self .__conn = http .client .HTTPConnection (self .__url .hostname , port , timeout = timeout )
106
87
107
88
def __getattr__ (self , name ):
108
89
if name .startswith ('__' ) and name .endswith ('__' ):
@@ -124,14 +105,14 @@ def _request(self, method, path, postdata):
124
105
try :
125
106
self .__conn .request (method , path , postdata , headers )
126
107
return self ._get_response ()
127
- except httplib .BadStatusLine as e :
128
- if e .line == "''" : # if connection was closed, try again
108
+ except http . client .BadStatusLine as e :
109
+ if e .line == "''" : # if connection was closed, try again
129
110
self .__conn .close ()
130
111
self .__conn .request (method , path , postdata , headers )
131
112
return self ._get_response ()
132
113
else :
133
114
raise
134
- except (BrokenPipeError ,ConnectionResetError ):
115
+ except (BrokenPipeError , ConnectionResetError ):
135
116
# Python 3.5+ raises BrokenPipeError instead of BadStatusLine when the connection was reset
136
117
# ConnectionResetError happens on FreeBSD with Python 3.4
137
118
self .__conn .close ()
@@ -141,8 +122,8 @@ def _request(self, method, path, postdata):
141
122
def get_request (self , * args , ** argsn ):
142
123
AuthServiceProxy .__id_count += 1
143
124
144
- log .debug ("-%s-> %s %s" % (AuthServiceProxy .__id_count , self ._service_name ,
145
- json .dumps (args , default = EncodeDecimal , ensure_ascii = self .ensure_ascii )))
125
+ log .debug ("-%s-> %s %s" % (AuthServiceProxy .__id_count , self ._service_name ,
126
+ json .dumps (args , default = EncodeDecimal , ensure_ascii = self .ensure_ascii )))
146
127
if args and argsn :
147
128
raise ValueError ('Cannot handle both named and positional arguments' )
148
129
return {'version' : '1.1' ,
@@ -163,7 +144,7 @@ def __call__(self, *args, **argsn):
163
144
164
145
def batch (self , rpc_call_list ):
165
146
postdata = json .dumps (list (rpc_call_list ), default = EncodeDecimal , ensure_ascii = self .ensure_ascii )
166
- log .debug ("--> " + postdata )
147
+ log .debug ("--> " + postdata )
167
148
return self ._request ('POST' , self .__url .path , postdata .encode ('utf-8' ))
168
149
169
150
def _get_response (self ):
@@ -190,9 +171,9 @@ def _get_response(self):
190
171
response = json .loads (responsedata , parse_float = decimal .Decimal )
191
172
elapsed = time .time () - req_start_time
192
173
if "error" in response and response ["error" ] is None :
193
- log .debug ("<-%s- [%.6f] %s" % (response ["id" ], elapsed , json .dumps (response ["result" ], default = EncodeDecimal , ensure_ascii = self .ensure_ascii )))
174
+ log .debug ("<-%s- [%.6f] %s" % (response ["id" ], elapsed , json .dumps (response ["result" ], default = EncodeDecimal , ensure_ascii = self .ensure_ascii )))
194
175
else :
195
- log .debug ("<-- [%.6f] %s" % (elapsed ,responsedata ))
176
+ log .debug ("<-- [%.6f] %s" % (elapsed , responsedata ))
196
177
return response
197
178
198
179
def __truediv__ (self , relative_uri ):
0 commit comments