码迷,mamicode.com
首页 > Windows程序 > 详细

windows下端口开放情况

时间:2015-10-13 17:14:32      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

这个小程序实现了在windows下,探测目前有哪些端口开放。

可以探测本机端口,也可输入自定义IP。

参考网址:

扫描端口情况:

http://bbs.chinaunix.net/thread-4180079-1-1.html

获取本机IP

http://blog.csdn.net/blade2001/article/details/3975389

 

# -*- coding:utf8 -*-
#!/usr/bin/python
# Python:          2.7
# Platform:        Windows
# Authro:          lyc
# Program:         端口扫描
# History:         2015.10.13
 
import socket, time, thread
socket.setdefaulttimeout(3)
 
def socket_port(ip,port,portList):
    """
    输入IP和端口号,扫描判断端口是否开放
    """
    try:
        if port >= 65535:
            print u端口扫描结束
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = s.connect_ex((ip,port))
        if result == 0:
            lock.acquire()
            print ip,u:,port,u端口开放
            portList.append(port)
            lock.release()
        s.close()
    except:
        print u端口扫描异常
 
def ip_scan(ip):
    """
    输入IP,扫描IP的0-65534端口情况
    """
    portList = []
    try:
        print u开始扫描 %s % ip
        start_time = time.time()
        for i in range(0,65534):
            thread.start_new_thread(socket_port,(ip,int(i),portList))
        print u扫描端口完成,总共用时 :%.2f %(time.time()-start_time)
        # 返回所有开放的端口号
        return portList
    except:
        print u扫描ip出错
         
 
if __name__==__main__:
    # 查看本机端口
    myname = socket.getfqdn(socket.gethostname())
    myaddr = socket.gethostbyname(myname)
    # 输入自定义IP
    # myaddr = raw_input(‘Input the ip you want to scan:\n‘)
    lock = thread.allocate_lock()
    ip_scan(myaddr)

 

windows下端口开放情况

标签:

原文地址:http://www.cnblogs.com/myblog-lyc/p/4875013.html

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