python检测远程主机的端口状态

本文出自 “峰云,就她了。” 博客,请务必保留此出处http://rfyiamcool.blog.51cto.com/1030776/1048707

#! /usr/bin/env python
#coding=utf-8
import socket

def main():
    sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sk.settimeout(1000)
    ip=raw_input("Please input ip(default:127.0.0.1)")
    if ip=="":
        ip="127.0.0.1"
    s=raw_input("Please input start port(default:80)")
    if s=="":
        startport=80
    else:
        startport=int(s)
        
    s=raw_input("Please input end port(default:80)")
    if s=="":
        endport=80
    else:
        endport=int(s)
        
    for port in range(startport,endport+1):
        print("Port scaning:%d"%port)
        try:
            sk.connect((ip,port))
            print("Server %s port %d OK!"%(ip,port))
        except Exception:
            print("Server %s port %d is not conneted"%(ip,port))
    sk.close()
    
if __name__=="__main__":
    main()
    
    

 

posted on 2013-05-20 00:29  寻步  阅读(645)  评论(0编辑  收藏  举报