@@ -92,6 +92,12 @@ class DarwinApi:
92
92
93
93
DEFAULT_TIMEOUT = 10
94
94
95
+ _SOCKET_PROTOCOL = {
96
+ 'unix' : (socket .AF_UNIX , socket .SOCK_STREAM ),
97
+ 'tcp' : (socket .AF_INET , socket .SOCK_STREAM ),
98
+ 'tcp6' : (socket .AF_INET6 , socket .SOCK_STREAM ),
99
+ }
100
+
95
101
@classmethod
96
102
def get_filter_code (cls , filter_name ):
97
103
"""
@@ -136,59 +142,45 @@ def __init__(self, **kwargs):
136
142
137
143
socket_type = kwargs .get ("socket_type" , None )
138
144
139
- if not socket_type or (socket_type .lower () != "tcp" and socket_type .lower () != "unix" ):
140
- raise DarwinInvalidArgumentError ("DarwinApi:: __init__:: You must give a socket type (tcp/unix)" )
141
-
142
145
try :
143
146
darwin_timeout = kwargs ["timeout" ]
144
147
145
148
except KeyError :
146
149
darwin_timeout = self .DEFAULT_TIMEOUT
147
150
148
151
self .socket = None
152
+ connection_info = None
149
153
150
154
if socket_type == "unix" :
151
- self .socket = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
152
- darwin_socket_path = kwargs .get ("socket_path" , None )
153
-
154
- if darwin_socket_path is None :
155
+ connection_info = kwargs .get ("socket_path" , None )
156
+ if connection_info is None :
155
157
raise DarwinInvalidArgumentError ("DarwinApi:: __init__:: No socket path has been given" )
156
-
157
- self .socket .setblocking (False )
158
- self .socket .settimeout (darwin_timeout )
159
-
160
158
if self .verbose :
161
- print ("DarwinApi:: __init__:: Connecting to " + str (darwin_socket_path ) + "..." )
159
+ print ("DarwinApi:: __init__:: Connecting to " + str (connection_info ) + "..." )
162
160
163
- try :
164
- self .socket .connect (darwin_socket_path )
165
- except socket .error as error :
166
- raise DarwinConnectionError (str (error ))
167
-
168
- else :
169
- self .socket = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
161
+ elif socket_type in self ._SOCKET_PROTOCOL :
170
162
darwin_socket_host = kwargs .get ("socket_host" , None )
171
163
darwin_socket_port = kwargs .get ("socket_port" , None )
172
-
173
164
if darwin_socket_host is None :
174
165
raise DarwinInvalidArgumentError ("DarwinApi:: __init__:: No socket host has been given" )
175
-
176
166
if darwin_socket_port is None :
177
167
raise DarwinInvalidArgumentError ("DarwinApi:: __init__:: No socket port has been given" )
178
-
179
- self .socket .setblocking (False )
180
- self .socket .settimeout (darwin_timeout )
181
-
168
+ connection_info = (darwin_socket_host , darwin_socket_port )
182
169
if self .verbose :
183
170
print ("DarwinApi:: __init__:: Connecting to {darwin_socket_host}: {darwin_socket_port}..." .format (
184
171
darwin_socket_host = darwin_socket_host ,
185
172
darwin_socket_port = darwin_socket_port ,
186
173
))
174
+ else :
175
+ raise DarwinInvalidArgumentError ("DarwinApi:: __init__:: Unknown socket_type provided" )
187
176
188
- try :
189
- self .socket .connect ((darwin_socket_host , darwin_socket_port ))
190
- except socket .error as error :
191
- raise DarwinConnectionError (str (error ))
177
+ try :
178
+ self .socket = socket .socket (* self ._SOCKET_PROTOCOL [socket_type ])
179
+ self .socket .setblocking (False )
180
+ self .socket .settimeout (darwin_timeout )
181
+ self .socket .connect (connection_info )
182
+ except socket .error as error :
183
+ raise DarwinConnectionError (str (error ))
192
184
193
185
def low_level_call (self , ** kwargs ):
194
186
"""
0 commit comments