hincky的主页 hincky的主页
  • 2023

    • nginx
    • prometheus
    • 小工具
    • 部署
  • 数据库

    • MySQL是怎么使用/运行的
    • Redis核心技术与实战
  • 极客时间

    • Web协议详解与抓包实战
    • SQL必知必会
    • MySQL45讲
个人日常
  • 分类
  • 标签
  • 归档
  • 随笔
GitHub (opens new window)

Hincky

当有趣的人,做想做的事
  • 2023

    • nginx
    • prometheus
    • 小工具
    • 部署
  • 数据库

    • MySQL是怎么使用/运行的
    • Redis核心技术与实战
  • 极客时间

    • Web协议详解与抓包实战
    • SQL必知必会
    • MySQL45讲
个人日常
  • 分类
  • 标签
  • 归档
  • 随笔
GitHub (opens new window)
  • 2022

  • 2023

    • 一月

    • 五月

    • nginx

    • prometheus

    • tools

      • 批量获取HTTP状态码
        • 批量获取HTTP状态码
          • 使用Shell编写:
          • 使用Python
          • 使用python,并将结果导出为csv文件
        • urls.txt
    • http状态码

    • 监控

    • Go

    • python

    • linux性能优化

    • PHP

    • elastic

    • chatgpt

    • 小工具

    • js

    • django

    • centos7.6配置

    • 部署

    • 部署资源整理

    • MySQL数据库

  • 工作记录
  • 2023
  • tools
hincky
2023-03-07
目录

批量获取HTTP状态码

  • 批量获取HTTP状态码
    • 使用Shell编写:
    • 使用Python
    • 使用python,并将结果导出为csv文件
  • urls.txt

# 批量获取HTTP状态码

# 使用Shell编写:

vim httpcode.py

#!/bin/bash

while read url; do
    status=$(curl -s -o /dev/null -w "%{http_code}" "$url")
    echo "$url - $status"
done < urls.txt

1
2
3
4
5
6
7

在这个脚本中,我们使用了curl命令来获取每个URL的HTTP状态码。-s选项表示静默模式,不输出进度条等信息。-o /dev/null表示将响应输出到空设备,以防止输出干扰我们的结果。-w "%{http_code}"表示输出HTTP状态码,格式为%{http_code}。最后,我们将每个URL的状态码与其URL一起输出。

我们使用了一个名为urls.txt的文件来存储要获取状态码的URL列表。您可以根据需要修改文件名和路径。

要使用该脚本,只需将要获取状态码的URL添加到urls.txt文件中,然后运行该脚本即可。

# 使用Python

import requests

with open("urls.txt", "r") as f:
    for url in f:
        url = url.strip()
        try:
            response = requests.head(url)
            status = response.status_code
            print(f"{url} - {status}")
        except requests.exceptions.RequestException as e:
            print(f"{url} - Failed to connect: {e}")

1
2
3
4
5
6
7
8
9
10
11
12

在这个代码中,我们使用了Python的requests库来获取每个URL的HTTP状态码。我们使用with语句打开一个名为urls.txt的文件,并使用for循环迭代每个URL。

对于每个URL,我们使用requests.head()方法发送一个HEAD请求,以获取响应的HTTP状态码。我们将状态码与URL一起输出。如果请求失败,我们将输出一个错误消息。

请注意,与Shell脚本不同,我们在Python中使用了requests.head()方法而不是requests.get()方法,这是因为我们只需要获取状态码而不需要响应的正文内容。

要使用该代码,只需将要获取状态码的URL添加到urls.txt文件中,然后运行该代码即可。

创建了urls.txt文件之后,直接运行python3 httpcode.py即可

# 使用python,并将结果导出为csv文件

import csv
import requests

with open("urls.txt", "r") as f, open("status_codes.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["URL", "Status Code"])
    for url in f:
        url = url.strip()
        try:
            response = requests.head(url)
            status = response.status_code
            writer.writerow([url, status])
        except requests.exceptions.RequestException as e:
            writer.writerow([url, f"Failed to connect: {e}"])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

在这个代码中,我们首先打开一个名为urls.txt的文件以读取要获取状态码的URL列表。然后,我们打开一个名为status_codes.csv的文件以写入结果。

我们使用Python的csv库来创建一个csv.writer对象,用于将结果写入CSV文件。我们使用writerow()方法将标题行写入文件。

对于每个URL,我们使用requests.head()方法发送一个HEAD请求以获取响应的HTTP状态码。我们将URL和状态码写入CSV文件中。如果请求失败,我们将输出一个错误消息。

请注意,在打开CSV文件时,我们使用了newline=""参数来避免在写入CSV文件时出现额外的换行符。如果您正在使用Python 2.x,请将newline=""替换为"wb"。

要使用该代码,只需将要获取状态码的URL添加到urls.txt文件中,然后运行该代码即可。结果将写入名为status_codes.csv的CSV文件中。

# urls.txt

http://nuxtpc.feicuizu.com
http://nuxtpc.feicuizu.com/zhubao/
http://nuxtpc.feicuizu.com/zixun
http://nuxtpc.feicuizu.com/news/413fb0b3a9506346.html
http://nuxtpc.feicuizu.com/article/8bac421b2c736afb.html
http://nuxtpc.feicuizu.com/jingxuan/509a2e6640df5691.html
http://nuxtpc.feicuizu.com/jingxuan/509a2e6640df5691sm.html
http://nuxtpc.feicuizu.com/jingxuan/509a2e6640df5691sg.html
http://nuxtpc.feicuizu.com/jingxuan/509a2e6640df5691tt.html
http://nuxtpc.feicuizu.com/jingxuan/509a2e6640df5691360.html
http://nuxtpc.feicuizu.com/ask/
http://nuxtpc.feicuizu.com/asks/5ed33243dce5e.html
http://nuxtpc.feicuizu.com/baikes/list_1
http://nuxtpc.feicuizu.com/baike/y_ganbai/
http://nuxtpc.feicuizu.com/brand/list1/
http://nuxtpc.feicuizu.com/brands/2d14dbcf2d11440.html
http://nuxtpc.feicuizu.com/s/
http://nuxtpc.feicuizu.com/s/100015fc35eb3e62f1/
http://nuxtpc.feicuizu.com/shop
http://nuxtpc.feicuizu.com/shop/elist_a0s0
http://nuxtpc.feicuizu.com/shope/5ed826d90d314.html
http://nuxtpc.feicuizu.com/shop/mlist_a0s0
http://nuxtpc.feicuizu.com/shopm/bcbc7159bf5202986bc4.html
http://nuxtpc.feicuizu.com/tag/
http://nuxtpc.feicuizu.com/tag/47bacd120c706186/
http://nuxtpc.feicuizu.com/jingxuan/197b455f20506692.html
http://nuxtpc.feicuizu.com/star/
http://nuxtpc.feicuizu.com/stars/5f18fa0454da5.html
http://nuxtpc.feicuizu.com/zhishitang/
http://nuxtpc.feicuizu.com/special/b703e574b0506a87.html
http://nuxtpc.feicuizu.com/map/
http://nuxtpc.feicuizu.com/g/
http://nuxtpc.feicuizu.com/goods/26887aa97a1f5fa4.html
http://nuxtpc.feicuizu.com/abouts.html


https://pc.demo.feicuishe.com/v/
https://pc.demo.feicuishe.com/v/a055127a3dff524c/


http://nuxt2m.feicuizu.com
http://nuxt2m.feicuizu.com/zhubao/
http://nuxt2m.feicuizu.com/zixun
http://nuxt2m.feicuizu.com/news/413fb0b3a9506346.html
http://nuxt2m.feicuizu.com/article/8bac421b2c736afb.html
http://nuxt2m.feicuizu.com/jingxuan/509a2e6640df5691.html
http://nuxt2m.feicuizu.com/jingxuan/509a2e6640df5691sm.html
http://nuxt2m.feicuizu.com/jingxuan/509a2e6640df5691sg.html
http://nuxt2m.feicuizu.com/jingxuan/509a2e6640df5691tt.html
http://nuxt2m.feicuizu.com/jingxuan/509a2e6640df5691360.html
http://nuxt2m.feicuizu.com/ask/
http://nuxt2m.feicuizu.com/asks/5ed33243dce5e.html
http://nuxt2m.feicuizu.com/baikes/list_1
http://nuxt2m.feicuizu.com/baike/y_ganbai/
http://nuxt2m.feicuizu.com/brand/list1/
http://nuxt2m.feicuizu.com/brands/2d14dbcf2d11440.html
http://nuxt2m.feicuizu.com/s/
http://nuxt2m.feicuizu.com/s/100015fc35eb3e62f1/
http://nuxt2m.feicuizu.com/shop/elist_a0s0
http://nuxt2m.feicuizu.com/shope/5ed826d90d314.html
http://nuxt2m.feicuizu.com/shop/mlist_a0s0
http://nuxt2m.feicuizu.com/shopm/bcbc7159bf5202986bc4.html
http://nuxt2m.feicuizu.com/tag/
http://nuxt2m.feicuizu.com/tag/47bacd120c706186/
http://nuxt2m.feicuizu.com/star/
http://nuxt2m.feicuizu.com/stars/5f18fa0454da5.html
http://nuxt2m.feicuizu.com/zhishitang/
http://nuxt2m.feicuizu.com/special/b703e574b0506a87.html
http://nuxt2m.feicuizu.com/map/
http://nuxt2m.feicuizu.com/g/
http://nuxt2m.feicuizu.com/goods/26887aa97a1f5fa4.html
http://nuxt2m.feicuizu.com/abouts
https://m.demo.feicuishe.com/v/
https://m.demo.feicuishe.com/v/a055127a3dff524c/
http://nuxt2m.feicuizu.com/koubei/
http://nuxt2m.feicuizu.com/settled/
http://nuxt2m.feicuizu.com/industry_jianding/
http://nuxt2m.feicuizu.com/industry_xunbao/
http://nuxt2m.feicuizu.com/single_yuanshi/
http://nuxt2m.feicuizu.com/industry_dingzhi/
http://nuxt2m.feicuizu.com/industry_gujia/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
编辑 (opens new window)
PromQL
状态码以及导致原因

← PromQL 状态码以及导致原因→

最近更新
01
集成chatgpt的工具
05-24
02
修改服务器ssh默认连接端口
05-22
03
阿里云免费证书
05-15
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Hincky | MIT License | 粤ICP备2022120427号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式