批量获取URL对应的IP

批量获取URL对应的IP

#codeing:utf-8

import urllib
import re
import sys
import os
import socket
import threading
import Queue
import csv


THREAD_NUM = 20

def readUrl(file):
    urllist = list()
    if os.path.exists(file):
        with open(file,'r') as f:
            for line in f:
                urllist.append(line.strip('\n'))
    return urllist



def getIPfromUrl(urllist):
    for url in urllist:
        protocol, s1 = urllib.splittype(url)
        host, s2 = urllib.splithost(s1)
        host, port = urllib.splitport(host)
        if port == None:
            port = 80
        addr = socket.getaddrinfo(host, protocol)
        ip = addr[0][4][0]
        print("{} : {}".format(url,ip))
        data = [url, ip]
        with open(cur_dir + '\\urlDir\\url&ip.csv', 'ab') as f:
            write = csv.writer(f)
            write.writerow(data)


if __name__ == '__main__':
    cur_dir = sys.path[0]
    file = cur_dir + '\\urlDir\\url.txt'
    if not os.path.exists(file):
        print "{} does not exist ".format(file)
        exit()

    urllist = readUrl(file)
    getIPfromUrl(urllist)


发布了30 篇原创文章 · 获赞 13 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/u013224189/article/details/85436748