1010import platform
1111
1212
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 ):
1414 """
1515 Check internet connections.
1616
17- :param host: the host that check connection to
17+ :param host: the host that check connection to
1818 :type host:str
19- :param port: port that check connection with
19+ :param port: port that check connection with
2020 :type port:int
21- :param timeout: times that check the connection
21+ :param timeout: times that check the connection
2222 :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
2426 >>> internet() # if there is stable internet connection
2527 True
2628 >>> internet() # if there is no stable internet connection
@@ -30,16 +32,17 @@ def internet(host="8.8.8.8", port=53, timeout=3):
3032 socket .setdefaulttimeout (timeout )
3133 socket .socket (socket .AF_INET , socket .SOCK_STREAM ).connect ((host , port ))
3234 return True
33- except Exception as ex :
34- print (str (ex ))
35+ except Exception as e :
36+ if debug :
37+ print (str (e ))
3538 return False
3639
3740
3841def local_ip (debug = False ):
3942 """
4043 Return local ip of computer in windows by socket module and in unix with hostname command in shell.
4144
42- :param debug:flag for using debug Mode
45+ :param debug:flag for using debug mode
4346 :type debug:bool
4447 :return: local ip as string
4548 """
@@ -180,3 +183,57 @@ def mac(debug=False):
180183 if debug :
181184 print (str (e ))
182185 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