#coding=utf-8
import requests
import traceback
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-s", help="src_path_read_file", )
parser.add_argument("-d", help="dst_path_write_file", )
args = parser.parse_args()
src_path_file = args.s
dst_path_file = args.d
print '读取目录:',src_path_file
print '写入目录:',dst_path_file
# src_path = '/mnt/test_interface_03/test/ip01.txt'
src_path = src_path_file
# filename = '/mnt/test_interface_03/test/write_data.txt'
filename = dst_path_file
def get_data(inputIP):
headers = {
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36"}
url = "http://tooly.1x1.ooo/index.php?ip=" + inputIP
r = requests.get(url, params=inputIP, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
# print 'r.text = ',r.text
# inputIP = raw_input("Enter You Check IP Address : ")
# html = get_data(inputIP)
try:
with open(src_path, 'r') as f:
a = f.read()
ip_list = [i for i in a.splitlines() if i != '']
print '需要查询的IP总数是:%d\n' % len(ip_list)
k = 1
for ip in ip_list:
print '正在查询第 %d 个IP:%s' % (k, ip)
# print '查询的ip是:%s' % ip #
html = get_data(ip).encode('utf-8')
print 'html = ',html #
print '正在写入第 %d 个IP的查询结果!' % k
with open(filename, 'aw') as f: # 如果filename不存在会自动创建, 'w'表示写数据,写之前会清空文件中的原有数据!
f.write('['+str(k)+']'+':'+ip+':'+html+'\n\n')
print '第 %d 个IP的查询结果写入完毕!\n' % k
k += 1
print '查询完毕!。。。。。。。。。\n\n\n'
except Exception as e:
traceback.print_exc()
可传入参数:
-s指定读取目录
-d指定写入目录
例如: python test.py -s 'src_fileName.txt' -d 'dst_fileName.txt'
注:运行环境python2.7