学习如何使用 http stat 查找网站性能


HTTP state 是一个 Python3 脚本,用于可视化 web curl(1) 统计信息。此脚本是用单个文件编写的,无需依赖项。本文解释了如何使用 http stat 来找出网站的性能。

安装 httpstat

如下所示,有两种方法可以在 Ubuntu 上安装 httpstat:

  • 直接下载脚本。
  • 通过 Python pip。

从脚本安装

要从直接脚本安装 httpstat,请使用以下命令:

$wget https://raw.githubusercontent.com/reorx/httpstat/master/httpstat.py

示例输出应如下所示:

--2016-12-19 11:27:18-- https://raw.githubusercontent.com/reorx/httpstat/master/httpstat.py
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10577 (10K) [text/plain]
Saving to: ‘httpstat.py’

httpstat.py 100%[===================>] 10.33K --.-KB/s in 0.02s

2016-12-19 11:27:20 (433 KB/s) - ‘httpstat.py’ saved [10577/10577]

从 Python pip 安装

在安装 httpstat 之前,Ubuntu 上需要一个 python-pip 包。要安装 python-pip 包,请使用以下命令:

$sudo apt install python-pip

示例输出应如下所示:

Building dependency tree
Reading state information... Done
The following additional packages will be installed:
   libexpat1-dev libpython-all-dev libpython-dev libpython2.7 libpython2.7-dev
   libpython2.7-minimal libpython2.7-stdlib python-all python-all-dev
   python-dev python-pip-whl python-pkg-resources python-setuptools
   python-wheel python2.7 python2.7-dev python2.7-minimal
Suggested packages:
   python-setuptools-doc python2.7-doc binfmt-support
The following NEW packages will be installed:
   libexpat1-dev libpython-all-dev libpython-dev libpython2.7-dev python-all
   python-all-dev python-dev python-pip python-pip-whl python-pkg-resources
   python-setuptools python-wheel python2.7-dev
The following packages will be upgraded:
   libpython2.7 libpython2.7-minimal libpython2.7-stdlib python2.7
   python2.7-minimal
5 upgraded, 13 newly installed, 0 to remove and 284 not upgraded.
Need to get 29.8 MB/34.6 MB of archives.
After this operation, 45.1 MB of additional disk space will be used.
......................................................................................

要安装 httpstat,请使用以下命令:

$ pip install httpstat

示例输出应如下所示:

Collecting httpstat
   Downloading httpstat-1.2.1-py2-none-any.whl
Installing collected packages: httpstat
Successfully installed httpstat

httpstat 的用法

如果您通过直接脚本安装了 httpstat,请使用以下命令:

$ python httpstat.py tutorialspoint.com

示例输出应如下所示:

Connected to 117.18.237.191:80 from 192.168.1.182:58492

HTTP/1.1 200 OK
Accept-Ranges: bytes
Access-Control-Allow-Headers: X-Requested-With
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=UTF-8
Date: Mon, 19 Dec 2016 06:09:15 GMT
Last-Modified: Mon, 19 Dec 2016 05:20:54 GMT
Server: ECS (pnq/AF9C)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 92840

Body stored in: /tmp/tmpvwKIfK

DNS Lookup             TCP Connection             Server Processing             Content Transfer
[ 253ms          |          16ms       |                15ms       |                   32ms ]
                 |                     |                           |                        |
            namelookup:253ms           |                           |                        |
                                connect:269ms                      |                        |
                                                           starttransfer:284ms              |
                                                                                       total:316ms

通过 Python Pip 使用 httpstat

如果您通过 python pip 安装了 httpstat,请使用以下命令:

$ httpstat sendfiles.net

示例输出应如下所示:

Connected to 115.112.255.70:80 from 192.168.1.182:46878
HTTP/1.1 200 OK
Date: Mon, 19 Dec 2016 06:11:59 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips PHP/5.4.16
X-Powered-By: PHP/5.4.16
Set-Cookie: PHPSESSID=ftvfqeu3gnr05a95hc2donbv16; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: User-Agent
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

Body stored in: /tmp/tmpq7FNyi

DNS Lookup                TCP Connection               Server Processing           Content Transfer
[ 254ms                |        1ms        |              6ms             |                  2ms ]
                       |                   |                              |                      |
                namelookup:254ms           |                              |                      |
                                  connect:255ms                           |                      |
starttransfer:261ms |
total:263ms

输出解释

以上示例给出了 tutorialspoint.com 和 sendfiles.net 的输出。它包含有关日期、服务器、X-Powered-by 方式、Set-Cookie、vary、content material-kind 以及许多其他查找网页统计信息的宝贵信息。

httpstat 中有很多选项可用,要了解更多关于 httpstat 的信息,请使用以下命令:

$ httpstat --help

示例输出应如下所示:

Usage: httpstat URL [CURL_OPTIONS]
httpstat -h | --help
httpstat --version

Arguments:
URL url to request, could be with or without `http(s)://` prefix

Options:
CURL_OPTIONS any curl supported options, except for -w -D -o -S -s,
which are already used internally.
-h --help show this screen.
--version show version.

Environments:
   HTTPSTAT_SHOW_BODY      Set to `true` to show resposne body in the output,
                           note that body length is limited to 1023 bytes, will be
                           truncated if exceeds. Default is `false`.
   HTTPSTAT_SHOW_IP        By default httpstat shows remote and local IP/port address.
                           Set to `false` to disable this feature. Default is `true`.
   HTTPSTAT_SHOW_SPEED     Set to `true` to show download and upload speed.
                           Default is `false`.
   HTTPSTAT_SAVE_BODY      By default httpstat stores body in a tmp file,
                           set to `false` to disable this feature. Default is `true`
   HTTPSTAT_CURL_BIN       Indicate the curl bin path to use. Default is `curl`
                           from current shell $PATH.
   HTTPSTAT_DEBUG          Set to `true` to see debugging logs. Default is `false`

以上命令提供了关于 httpstat 及其用法选项的清晰信息。阅读本文后,您将能够理解如何在 Ubuntu 上安装 httpstat 以找出网站性能统计信息。在我们的下一篇文章中,我们将介绍更多基于 Linux 的技巧和提示。继续关注!

更新于:2020年1月21日

162 次浏览

启动您的职业生涯

完成课程后获得认证

开始
广告