有关更多信息以及 curl 后的干净新行
~/.curlrc
-w "\nstatus=%{http_code} %{redirect_url} size=%{size_download} time=%{time_total} content-type=\"%{content_type}\"\n"
(此处提供更多选项)
redirect_url
如果请求没有被重定向或您使用-L
跟随重定向,则将为空白。
示例输出:
~ ➤ curl https://www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.co.uk/?gfe_rd=cr&ei=FW">here</A>.
</BODY></HTML>
status=302 https://www.google.co.uk/?gfe_rd=cr&ei=FW size=262 time=0.044209 content-type="text/html; charset=UTF-8"
~ ➤
编辑,为了使内容更具可读性,您可以将 ANSI 颜色添加到该-w
行,直接编写并不容易,但是此脚本可以生成~/.curlrc
带有颜色的文件。
#!/usr/bin/env python3
from pathlib import Path
import click
chunks = [
('status=', 'blue'),
('%{http_code} ', 'green'),
('%{redirect_url} ', 'green'),
('size=', 'blue'),
('%{size_download} ', 'green'),
('time=', 'blue'),
('%{time_total} ', 'green'),
('content-type=', 'blue'),
('\\"%{content_type}\\"', 'green'),
]
content = '-w "\\n'
for chunk, colour in chunks:
content += click.style(chunk, fg=colour)
content += '\\n"\n'
path = (Path.home() / '.curlrc').resolve()
print('writing:\n{}to: {}'.format(content, path))
path.write_text(content)