网站首页 > 技术教程 正文
一、利用python批量测试网站是否可正常被访问
应用场景:当有大量网站需要检测是否能正常打开时,可以采用此方式;
import requests
#创建函数netcheck,传入参数为url,即需要被访问的网址
def netcheck(url):
try:
#利用requests.get访问url地址,timeout为超时参数,单位为秒;r.status_code为请求状态码
r = requests.get(url, timeout = 1)
status_code = r.status_code
return status_code
except Exception as e:
return e
if __name__ == "__main__":
#需要准备好都是url 的文件,每个url一行,打开一个txt文件,每次读入一行line
with open("urllist.txt") as f:
try:
for line in f:
#strip() 移除字符串开头和结尾的空格和换行符
status = netcheck(line.strip())
if status == 200:
print(line.strip() + ': successful')
#打开一个txt文件,以追加的方式(a),将请求状态码为200的url写入到一个文档中
with open('valid_feedlist.txt', 'a') as f1:
f1.write(line)
else:
#打开另一个txt文件,以追加的方式(a),将请求状态码为不是200的url写入到一个文档中
print(line.strip()+': unsuccessful')
with open('invalid_feedlist.txt', 'a') as f2:
f2.write(line)
except Exception as e:
print (e)
不足之处:对于网站中子模块是否能正常访问,并不能检测到;
他山之石:利用【Xenu】这个软件,可以对一个网站中所有的连接进行尝试访问,并给出结果。
二、利用python批量获得linux系统时间
应用场景:当有一批服务器时,需要查看各个服务器上的系统时间;
#-*- coding: utf-8 -*-
import paramiko
import threading
import time
def ssh2(ip,username,passwd,cmd):
try:
ssh = paramiko.SSHClient() #创建ssh对象
#允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,'22',username,passwd,timeout=5)
for m in cmd:
#exec_command 执行命令并获取命令结果.
#stdin为输入的命令,stdout为命令返回的结果,stderr为命令错误时返回的结果
stdin, stdout, stderr = ssh.exec_command(m)
#stdin.write("Y") #简单交互,输入 ‘Y’
out = stdout.readlines()
if (m=='date'):
#out是个list,转成str,"".join(out)
result1 = ip+ "=="+"".join(out)
#print (result1)
with open('timecheckresult.txt','a') as f1:
f1.write(result1)
else:
#获得当前时间,并转成时间戳
current_timestampe = int(time.time())
os_timestampe = int("".join(out))
#当前时间和系统时间差5秒,则输出服务器IP和服务器时间和当前时间
if (current_timestampe-os_timestampe) >5:
os_time = time.localtime(os_timestampe)
os_time_time = time.strftime("%Y--%m--%d %H:%M:%S", os_time)
current_time = time.localtime(current_timestampe)
current_time_time = time.strftime("%Y--%m--%d %H:%M:%S", current_time)
result2 = "current_time=="+current_time_time+"==="+ip+ "==server-time==" + os_time_time+'\n'
print(result2)
with open('timecheckresult_dif.txt','a') as f2:
f2.write(result2)
ssh.close()
except :
print (stdin.readlines())
print (stderr.readlines())
print ('%s\tError\n'%(ip))
if __name__=='__main__':
# cmd 你要执行的命令列表;在linux中date +%s返回时间戳,单位为秒
cmd = ['date','date +%s']#你要执行的命令列表
# 读取存储ip、用户名、密码的文件,循环执行
with open("ipandpass.txt") as f:
lines = f.readlines()
for i in range(len(lines)):
cols = lines[i].split()
ip = cols[0]
passwd = cols[1]
username='root'
#单线程
ssh2(ip,username,passwd,cmd)
#threads = [] #多线程
#a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
#a.start()
扩展应用:可以执行date命令,那也可以执行其他命令,比如内存,存储,性能等
- 上一篇: 星牛网提醒您SEO必需会五种工具 星牛旅行官网
- 下一篇: 如何检测网站死链? 查死链接工具
猜你喜欢
- 2024-10-18 诠网科技|网站seo排名波动原因分析及恢复排名对策
- 2024-10-18 2018.12.19(20)产品页面优化以及头部优化
- 2024-10-18 web测试之功能测试常用的方法有哪几种?有什么要点要注意?
- 2024-10-18 网站改版该怎么做SEO优化? 网站改版方案
- 2024-10-18 合肥网络营销:网站死链检测与处理方法
- 2024-10-18 软件测试中18个功能测试点总结(知识篇)
- 2024-10-18 如何加快网站的响应速度 如何加快网站的访问速度
- 2024-10-18 Google Search Console报告收到5XX错误时的含义以及如何处理
- 2024-10-18 新站怎么做SEO优化 新网站怎么优化seo
- 2024-10-18 网站死链接怎么处理 死链接测试
你 发表评论:
欢迎- 05-05从virsh当中学习QEMU/KVM启动命令
- 05-05Win10 BCD文件损坏怎么修复?(bcd文件损坏win7)
- 05-05亚马逊春节假期期间的店铺管理设置
- 05-051分钟总结常用k8s常用诊断教程(k8s常见故障)
- 05-05VisiPics重复图片查找软件中文汉化教程
- 05-05微服务的发布实现方式1灰度实现(微服务实现原理)
- 05-05轻松掌握Java多线程 - 第二章:线程的生命周期
- 05-05德拉诺之王邪DK报告:PVE向小测试及分析
- 最近发表
- 标签列表
-
- sd分区 (65)
- raid5数据恢复 (81)
- 地址转换 (73)
- 手机存储卡根目录 (55)
- tcp端口 (74)
- project server (59)
- 双击ctrl (55)
- 鼠标 单击变双击 (67)
- debugview (59)
- 字符动画 (65)
- flushdns (57)
- ps复制快捷键 (57)
- 清除系统垃圾代码 (58)
- web服务器的架设 (67)
- 16进制转换 (69)
- xclient (55)
- ps源文件 (67)
- filezilla server (59)
- 句柄无效 (56)
- word页眉页脚设置 (59)
- ansys实例 (56)
- 6 1 3固件 (59)
- sqlserver2000挂起 (59)
- vm虚拟主机 (55)
- config (61)
本文暂时没有评论,来添加一个吧(●'◡'●)