#!/usr/bin/env python
# -*- coding: gb18030 -*-
#site: www.jb200.com
#
from socket import *
import os
import sys
type = sys.getfilesystemencoding()
 
#从文件中读取扫描端口
def getPort(fileroute = "PORTS.txt"):
    list={}
    fileobject = open(fileroute,'r')
    for eachLine in fileobject:
        strs = str(eachLine).split('=')
        if len(strs) > 1 :
            list[int(strs[0])] = strs[1]
    return list
 
#主程序
list = getPort()
keys = list.keys()
keys.sort()
 
HOST = sys.argv[0]
fileobject = open("D:result1.txt",'w')
tcpCliSock = socket(AF_INET,SOCK_STREAM)
result = ''
 
#list_sort = sorted(list, key = lambda d:d[0], reverse = False)
 
for p in keys:     
    try:
        tcpCliSock = socket(AF_INET,SOCK_STREAM)
        tcpCliSock.connect((HOST,int(p)))
        tcpCliSock.close()
        del tcpCliSock
        result = str(p)+" -> " + list[p]
        print result
        fileobject.writelines([result])
     
    except error:
        print str(p) + " -> not open"
        
continue