OLINK CLOUD 针对中国大陆优化线路高性价比VPS

德国CN2-GIA三网优化回国线路vps,最低每月5$起,点击购买 (终身9折优惠码:OLINK)

使用 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
#!/usr/bin/env python
#encoding:utf8
#author: linuxhub.org
#Python检测IP端口连接是否正常

import socket

def is_open(ip,port):
s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((ip,int(port)))
s.shutdown(2)
return True
except:
return False


if __name__ == '__main__':

host = '192.168.0.202'
port = '6379'

if is_open(host, port):
print "OK"
else:
print "NO"

评论