# -*- coding: utf-8 -*-
import requests
import urllib3
def get_code_status(urls_file):
"""
检测urls状态码
:param urls_file: urls文件
:return: 状态码
allow_redirects: 拒绝默认的301/302重定向从而可以通过 html.headers[‘Location’] 拿到重定向的 URL。
verify: 取消证书认证
"""
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3298.4 Safari/537.36'
}
with open(urls_file, 'r', encoding='utf-8') as f:
urls_data = f.readlines()
for url in urls_data:
url = url.strip('\n')
disable_warnings()
try:
res = requests.get(
url, headers=header, verify=False, allow_redirects=False
)
except Exception as error:
print(error)
code = res.status_code
if code != 200:
print('状态码 {}:{}'.format(code, url))
return code
def disable_warnings():
"""
解除去掉证书后总是抛出异常告警
"""
urllib3.disable_warnings()
if __name__ == '__main__':
get_code_status('./urls.txt')
Python3 批量检查URL状态(status code) 是否为200 OK
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《Python3 批量检查URL状态(status code) 是否为200 OK》
文章链接:https://piaoyun.cc/1511.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途云路窝农家乐,否则法律问题自行承担。
文章名称:《Python3 批量检查URL状态(status code) 是否为200 OK》
文章链接:https://piaoyun.cc/1511.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途云路窝农家乐,否则法律问题自行承担。
相关推荐
Python开源代码txt文本文件拆分写入到多个txt文本文件的操作方法说明
ERROR: Cannot uninstall 'PyYAML'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall
python 安装第三方库,超时报错--Read timed out.
python pip源-PyPI使用国内源加速更新升级的设置方法
Windows PowerShell中无法将"python"项识别为cmdlet、函数、脚本文件或可运行程序的名称解决方法
解决python无法更新pip ModuleNotFoundError: No module named 'pip'的解决办法
ImportError: No module named setuptools 错误解决方案
goagent新功能个人配置文件proxy.user.ini使用简介