Python—端口检测

#!/usr/bin/evn python
#-*-coding:utf-8 -*-

import sys, socket
from argparse import ArgumentParser
from optparse import OptionParser

def net_is_used(port,ip='127.0.0.1'):
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
        s.connect((ip,port))
        s.shutdown(2)
        print('%s:%d is used' % (ip,port))
        return True
    except socket.error as e:
        print('%s:%d is unused' % (ip,port))
        return False


if __name__ == '__main__':
	args = sys.argv
	port = sys.argv[1];
    # backup = backupTools()
    
	# parser=OptionParser()
	# parser.set_usage('l love python')
	# parser.add_option('-d','--domain',dest='domainName',help=' put the domain name')
	# parser.add_option('-f','--file',dest='fileName',help=' put the fileName which cotains domains;one line one domain')
	# options,_=parser.parse_args()

	parser = ArgumentParser(description="TCP端口检测")
	parser.add_argument('ip')  
	parser.add_argument('-a', '--address', dest='address', default='localhost', help='address for the server')
	parser.add_argument('-p', '--port', dest="port", default=80, type=int, choices=[80,443,3306], help='port for the server')
	# args = parser.parse_args()
	args = parser.parse_known_args()[0]
	print "args---",args.ip, args.address, args.port
	net_is_used(int(args.port))

  https://www.cnblogs.com/yqmcu/p/9804002.html

https://www.jb51.net/article/144318.htm

https://blog.csdn.net/weixin_33888907/article/details/93054754   git

    原文作者:刘_love_田
    原文地址: https://www.cnblogs.com/liuhaidon/p/11704759.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞