學習如何使用 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 次瀏覽

啟動您的職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.