• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Blood_Zero
从新开始!从心开始!
   首页    新随笔       管理     

python端口扫描

简易版:

#author:Blood_Zero
#coding:utf-8

import socket
import sys

PortList=[21,22,23,25,80,135]
#

host=sys.argv[1]

for i in PortList:
    sock=socket.socket()
    sock.settimeout(2)
    try:
        sock.connect((host,i))
        print "the port %d is open" %i
    except Exception:
        print "the port %d is not open" %i
    sock.close()

执行方法:python port_scan.py www.xx.com

 

多线程端口扫描:

#author:Blood_Zero
#coding:utf-8

import socket
import multiprocessing
import sys

host=sys.argv[1]
PortList=[21,22,23,25,80,135]

#定义端口扫描函数
def scan(port):
    port=(int(port))
    sock=socket.socket()
    sock.settimeout(1)
    print "the port %d is scanning" %port
    try:
        sock.connect((host,port))
        print "the server %s port %d is open" %(host,port)
    except Exception:
        print "the server %s port %d is not open" %(host,port)
    sock.close()

# 主函数
if __name__=="__main__":
    thread=multiprocessing.Pool(processes=100)
    for i in PortList:
        thread.apply_async(scan(i))    
    thread.close()

执行方式 python port_scan.py www.xx.com

后续还会出很多加强版!

 

posted @ 2015-04-21 22:25  BloodZero  阅读(407)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3