码迷,mamicode.com
首页 > 编程语言 > 详细

Python—端口检测

时间:2019-10-19 19:07:17      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:desc   blog   str   def   port   int   rom   ==   server   

#!/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

Python—端口检测

标签:desc   blog   str   def   port   int   rom   ==   server   

原文地址:https://www.cnblogs.com/liuhaidon/p/11704759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!