美文网首页
python扫描端口

python扫描端口

作者: CSeroad | 来源:发表于2018-06-14 20:47 被阅读8次

刚学python没多长时间,写点好玩的脚本。使用python 2.7扫描端口。

话不多说,敲敲代码

#coding=utf-8

import socket

import time

ip=raw_input(unicode("请输入IP地址:","utf-8").encode("gbk"))

def scan(ip):

t=time.time()

print(u'开始扫描端口'+5*'>>>>>>')

for port in range(1,500):

try:

s=socket.socket()

s.settimeout(0.1)

s.connect((ip,port))

#80端口开放,连接正常,端口异常,代码报错

print('%s>>>>>>>>>>>open' % port)

s.close()

except:

pass

#print('%s>>>>>>>>>>>close' % port)

print(u'扫描端口完成。用时 time:%f' % (time.time()-t))

scan(ip)

因为python本身的缩进。所以我再附加张图吧!

test.py

附加结果:

success

相关文章

网友评论

      本文标题:python扫描端口

      本文链接:https://www.haomeiwen.com/subject/nsdueftx.html