本文实例为大家分享了python判断设备是否联网的具体代码,供大家参考,具体内容如下
直接上代码,就是用判断socket能不连上的方法来判断。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#!/usr/bin/env python # -*- coding: utf-8 -*- import socket def isNetOK(testserver): s = socket.socket() s.settimeout( 3 ) try : status = s.connect_ex(testserver) if status = = 0 : s.close() return True else : return False except Exception as e: return False def isNetChainOK(testserver = ( 'www.baidu.com' , 443 )): isOK = isNetOK(testserver) return isOK def isNetUSAOK(testserver = ( 'www.google.com' , 443 )): isOK = isNetOK(testserver) return isOK def isNetYouTubeOK(testserver = ( 'www.youtube.com' , 443 )): isOK = isNetOK(testserver) return isOK def main(): chinanet = isNetChainOK() print chinanet usanet = isNetUSAOK() print usanet youtubenet = isNetYouTubeOK() print youtubenet if __name__ = = '__main__' : main() |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/fengmm521/article/details/79469113