python怎么启动80端口_Python批量扫描服务器80端口状态

用Python写了一个简陋的端口扫描脚本,其简单的逻辑如下:

1. python DetectHostPort.py iplist.txt(存放着需要扫描的IP地址列表的文本,每行一个地址)

2. 输入扫描端口、扫描时间和扫描间隔。

3. 输出扫描信息。

下面贴上源码,欢迎拍砖。

#!/usr/bin/env python

importsysimporttimeimportsocketdefgetaddresslist(addr):"""getaddresslist(addr) -> IP address file

IP address read from the file.

:param addr: IP file

:return: Scan ip address list, or error message."""address=[]try:

with open(addr,"r") as iplist:

line=iplist.readlines()for item inline:

address.append(item.strip("\n"))returnaddressexcept(IOError, IndexError), e:returnstr(e)def scan(iplist, port=80):"""scan() -> getaddresslist()

getaddresslist() function returns the IP address of the list.

:param iplist: getaddresslist() Function return value.

:param port: Need to scan the port.

:return: None"""

if notisinstance(iplist, list):

sys.exit("Function getaddresslist() return error message: %s" %iplist)#start_time = time.time()

for addr iniplist:

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.settimeout(1)

host=(addr, int(port))try:

s.connect(host)print "Host %s:%s connection success." % (host[0], host[1])exceptException, e:print "Host %s:%s connection failure: %s" % (host[0], host[1], e)

s.close()

if __name__ == '__main__':

addrs= sys.argv[1]ScanPort= input("Enter the scan port:")

Total= input("Enter the scan time :")

Interval= input("Enter the scanning interval :")

EndTime= time.time() + Total * 60

while time.time()

scan(getaddresslist(addrs), ScanPort)

time.sleep(Interval* 60)continue

else:print "\nwhile end."

运行时只能扫描一个端口,但是可以对代码进行修改,扩展为扫描多个端口。

Python 的详细介绍:请点这里

Python 的下载地址:请点这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值