python判断端口是否打开的实现代码

416次阅读  |  发布于5年以前

复制代码 代码如下:

!/usr/bin/env python

name IsOpen.py

import os
import socket
def IsOpen(ip,port):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((ip,int(port)))
s.shutdown(2)
print '%d is open' % port
return True
except:
print '%d is down' % port
return False
if name == 'main':
IsOpen('1.1.1.1',800)

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8