10
10
import platform
11
11
12
12
13
- def internet (host = "8.8.8.8" , port = 53 , timeout = 3 ):
13
+ def internet (host = "8.8.8.8" , port = 53 , timeout = 3 , debug = False ):
14
14
"""
15
15
Check internet connections.
16
16
17
- :param host: the host that check connection to
17
+ :param host: the host that check connection to
18
18
:type host:str
19
- :param port: port that check connection with
19
+ :param port: port that check connection with
20
20
:type port:int
21
- :param timeout: times that check the connection
21
+ :param timeout: times that check the connection
22
22
:type timeout:int
23
- :return bool: True if Connection is Stable
23
+ :param debug:flag for using debug mode
24
+ :type debug:bool
25
+ :return bool: True if connection is stable
24
26
>>> internet() # if there is stable internet connection
25
27
True
26
28
>>> internet() # if there is no stable internet connection
@@ -30,16 +32,17 @@ def internet(host="8.8.8.8", port=53, timeout=3):
30
32
socket .setdefaulttimeout (timeout )
31
33
socket .socket (socket .AF_INET , socket .SOCK_STREAM ).connect ((host , port ))
32
34
return True
33
- except Exception as ex :
34
- print (str (ex ))
35
+ except Exception as e :
36
+ if debug :
37
+ print (str (e ))
35
38
return False
36
39
37
40
38
41
def local_ip (debug = False ):
39
42
"""
40
43
Return local ip of computer in windows by socket module and in unix with hostname command in shell.
41
44
42
- :param debug:flag for using debug Mode
45
+ :param debug:flag for using debug mode
43
46
:type debug:bool
44
47
:return: local ip as string
45
48
"""
@@ -180,3 +183,57 @@ def mac(debug=False):
180
183
if debug :
181
184
print (str (e ))
182
185
return GENERAL_ERROR_MESSAGE
186
+
187
+
188
+ def network_control (command , device = "eth0" , debug = False ):
189
+ """
190
+ Control network adaptor.
191
+
192
+ :param command: input command
193
+ :type command: str
194
+ :param device: network device name
195
+ :type device:str
196
+ :param debug: flag for using debug mode
197
+ :type debug:bool
198
+ :return: True in successful and False otherwise
199
+ """
200
+ try :
201
+ cmd = "up"
202
+ if command == "down" :
203
+ cmd = "down"
204
+ cmd_out = sub .Popen (["ifconfig" , device , cmd ],
205
+ stderr = sub .PIPE , stdin = sub .PIPE , stdout = sub .PIPE )
206
+ output = list (cmd_out .communicate ())
207
+ if len (output [0 ]) == 0 and len (output [1 ]) == 0 :
208
+ return True
209
+ return False
210
+ except Exception as e :
211
+ if debug :
212
+ print (str (e ))
213
+ return GENERAL_ERROR_MESSAGE
214
+
215
+
216
+ def network_enable (device = "eth0" , debug = False ):
217
+ """
218
+ Shortcut to enable network adaptor.
219
+
220
+ :param device: network device name
221
+ :type device:str
222
+ :param debug: flag for using debug mode
223
+ :type debug:bool
224
+ :return: True in successful and False otherwise
225
+ """
226
+ return network_control ("up" , device = device , debug = debug )
227
+
228
+
229
+ def network_disable (device = "eth0" , debug = False ):
230
+ """
231
+ Shortcut to disable network adaptor.
232
+
233
+ :param device: network device name
234
+ :type device:str
235
+ :param debug: flag for using debug mode
236
+ :type debug:bool
237
+ :return: True in successful and False otherwise
238
+ """
239
+ return network_control ("down" , device = device , debug = debug )
0 commit comments