# Httpie安装及使用
> 项目地址:https://httpie.io/ 中文文档:https://httpie.cn/
## Httpie简介
HTTPie(读作 *—aitch-tee-tee-pie—*)是一个命令行 HTTP 客户端,拥有直观的界面,支持 JSON、语法高亮、下载功能(类似 `wget`)、插件支持等特性。
![The same command side-by-side with cURL](https://httpie.cn/static/img/httpie2.png?v=1f6219a5a07bb6e99aa7afd98d0e67ec)
HTTPie 由一个简单的 `http` 命令构成,易于调试,可以很方便地与 HTTP 服务器、RESTful API 以及 Web Service 进行交互。HTTPie 主要功能特点包括:
- 提供了合理的默认选项
- 富有表现力和直观的命令语法
- 在终端支持高亮且格式化输出结果
- 内置 JSON 支持
- 支持持久会话存储
- 支持表单提交和文件上传
- 支持 HTTPS、代理以及身份授权验证
- 支持多种类型的请求数据和请求头
- 支持类似 `wget` 的下载模式
- 支持自定义扩展
- 支持 Linux、macOS 和 Windows 系统
- 更多特色等你发现…
[查看所有功能](https://httpie.cn/doc)
## 安装
一个通用的安装方式是使用 [pip](https://pip.pypa.io/en/stable/installing/)(可运行在 Windows、macOS 和 Linux 系统,并且会安装最新的版本):
```bash
# 确保使用的是最新版本的 pip 和 setuptools:
$ pip install --upgrade pip setuptools
$ pip install --upgrade httpie
```
如果安装失败,推荐使用清华镜像安装。
```bash
$ pip3 install --upgrade pip setuptools -i https://mirrors.aliyun.com/pypi/simple/
```
```bash
$ pip3 install --upgrade httpie -i https://mirrors.aliyun.com/pypi/simple/
```
查看是否安装成功
```bash
http --version
3.1.0
```
### 使用
自定义 [HTTP 方法](https://httpie.cn/doc#http-method)、[HTTP 头部](https://httpie.cn/doc#http-headers) 和 [JSON](https://httpie.cn/doc#json) 数据:
```bash
$ http PUT example.org X-API-Token:123 name=John
```
[表单](https://httpie.cn/doc#forms) 提交:
```bash
$ http -f POST example.org hello=World
```
使用 [输出选项](https://httpie.cn/doc#output-options) 来查看发送的请求信息:
```bash
$ http -v example.org
```
使用 [Github API](https://developer.github.com/v3/issues/comments/#create-a-comment) 向指定的 [issue](https://github.com/jakubroztocil/httpie/issues/83) 提交评论(需要带上 [授权验证](https://httpie.cn/doc#authentication) 参数):
```bash
$ http -a USERNAME POST https://api.github.com/repos/jakubroztocil/httpie/issues/83/comments body='HTTPie is awesome! :heart:'
```
使用 [重定向输入](https://httpie.cn/doc#redirected-input) 上传文件:
```bash
$ http example.org < file.json
```
使用 [重定向输出](https://httpie.cn/doc#redirected-output) 下载和保存文件:
```bash
$ http example.org/file > file
```
下载文件,类似 `wget` 写法:
```bash
$ http --download example.org/file
```
使用具名 [会话](https://httpie.cn/doc#sessions) 执行某项操作或对同一主机的请求进行持久通信:
```bash
$ http --session=logged-in -a username:password httpbin.org/get API-Key:123
$ http --session=logged-in httpbin.org/headers
```
设置自定义 `主机` 头信息处理丢失的 DNS 记录:
```bash
$ http localhost:8000 Host:example.com
```
# 附录1
## curl命令
**cURL**是一个利用URL语法在[命令行](https://baike.baidu.com/item/命令行)下工作的文件传输工具,1997年首次发行。它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下载工具。cURL还包含了用于程序开发的libcurl。
cURL支持的通信协议有[FTP](https://baike.baidu.com/item/FTP)、[FTPS](https://baike.baidu.com/item/FTPS)、[HTTP](https://baike.baidu.com/item/HTTP)、[HTTPS](https://baike.baidu.com/item/HTTPS)、[TFTP](https://baike.baidu.com/item/TFTP)、[SFTP](https://baike.baidu.com/item/SFTP)、[Gopher](https://baike.baidu.com/item/Gopher)、[SCP](https://baike.baidu.com/item/SCP)、[Telnet](https://baike.baidu.com/item/Telnet)、DICT、FILE、[LDAP](https://baike.baidu.com/item/LDAP)、LDAPS、[IMAP](https://baike.baidu.com/item/IMAP)、[POP3](https://baike.baidu.com/item/POP3)、[SMTP](https://baike.baidu.com/item/SMTP)和[RTSP](https://baike.baidu.com/item/RTSP)。
curl还支持SSL认证、HTTP POST、HTTP PUT、FTP上传, HTTP form based upload、proxies、HTTP/2、cookies、用户名+密码认证(Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos)、file transfer resume、proxy tunneling。
## Simple Usage
Get the main page from a web-server:
```
curl https://www.example.com/
```
Get the README file the user's home directory at funet's ftp-server:
```
curl ftp://ftp.funet.fi/README
```
Get a web page from a server using port 8000:
```
curl http://www.weirdserver.com:8000/
```
Get a directory listing of an FTP site:
```
curl ftp://ftp.funet.fi
```
Get the definition of curl from a dictionary:
```
curl dict://dict.org/m:curl
```
Fetch two documents at once:
```
curl ftp://ftp.funet.fi/ http://www.weirdserver.com:8000/
```
Get a file off an FTPS server:
```
curl ftps://files.are.secure.com/secrets.txt
```
or use the more appropriate FTPS way to get the same file:
```
curl --ftp-ssl ftp://files.are.secure.com/secrets.txt
```
Get a file from an SSH server using SFTP:
```
curl -u username sftp://example.com/etc/issue
```
Get a file from an SSH server using SCP using a private key (not password-protected) to authenticate:
```
curl -u username: --key ~/.ssh/id_rsa scp://example.com/~/file.txt
```
Get a file from an SSH server using SCP using a private key (password-protected) to authenticate:
```
curl -u username: --key ~/.ssh/id_rsa --pass private_key_password
scp://example.com/~/file.txt
```
Get the main page from an IPv6 web server:
```
curl "http://[2001:1890:1112:1::20]/"
```
Get a file from an SMB server:
```
curl -u "domain\username:passwd" smb://server.example.com/share/file.txt
```
更多curl命令用法参考:https://curl.se/docs/manual.html
## wget命令
wget 是一个从网络上自动下载文件的自由工具,支持通过 HTTP、HTTPS、FTP 三个最常见的 [TCP/IP协议](https://baike.baidu.com/item/TCP%2FIP协议) 下载,并可以使用 HTTP 代理。"wget" 这个名称来源于 “World Wide Web” 与 “get” 的结合。
所谓自动下载,是指 wget 可以在用户退出系统的之后在继续后台执行,直到下载任务完成。
GNU Wget is a command-line utility for downloading files from the web. With Wget, you can download files using HTTP, HTTPS, and FTP protocols. Wget provides a number of options allowing you to download multiple files, resume downloads, limit the bandwidth, recursive downloads, download in the background, mirror a website, and much more.
This article shows how to use the `wget` command through practical examples and detailed explanations of the most common options.
#### Installing Wget
The wget package is pre-installed on most Linux distributions today.
To check whether the Wget package is installed on your system, open up your console, type `wget`, and press enter. If you have wget installed, the system will print *`wget: missing URL`*. Otherwise, it will print *`wget command not found`*.
If `wget` is not installed, you can easily install it using the package manager of your distro.
#### Installing Wget on Ubuntu and Debian
```
sudo apt install wgetCopy
```
#### Installing Wget on CentOS and Fedora
```
sudo yum install wgetCopy
```
#### Wget Command Syntax
Before going into how to use the `wget` command, let’s start by reviewing the basic syntax.
The `wget` utility expressions take the following form:
```sh
wget [options] [url]
```
Copy
- `options` - The [Wget options](https://linux.die.net/man/1/wget)
- `url` - URL of the file or directory you want to download or synchronize.
#### How to Download a File with `wget`
In its simplest form, when used without any option, `wget` will download the resource specified in the [url] to the current directory.
In the following example, we are downloading the Linux kernel tar archive:
```
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.17.2.tar.xzCopy
```
![wget download file](https://linuxize.com/post/wget-command-examples/wget-download-file_hu0831c279ec53a04255c8fdf863c3c423_47896_768x0_resize_q75_lanczos.jpg)
As you can see from the image above, `wget` starts by resolving the domain’s IP address, then connects to the remote server and starts the transfer.
During the download, `wget` shows the progress bar alongside the file name, file size, download speed, and the estimated time to complete the download. Once the download is complete, you can find the downloaded file in your [current working directory](https://linuxize.com/post/current-working-directory/) .
To turn off the output, use the `-q` option.
If the file already exists, `wget` will add `.N` (number) at the end of the file name.
#### Saving the Downloaded File Under Different Name
To save the downloaded file under a different name, pass the `-O` option followed by the chosen name:
```
wget -O latest-hugo.zip https://github.com/gohugoio/hugo/archive/master.zipCopy
```
The command above will save the latest [hugo](https://gohugo.io/) zip file from GitHub as `latest-hugo.zip` instead of its original name.
#### Downloading a File to a Specific Directory
By default, `wget` will save the downloaded file in the current working directory. To save the file to a specific location, use the `-P` option:
```
wget -P /mnt/iso http://mirrors.mit.edu/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1804.isoCopy
```
The command above tells `wget` to save the CentOS 7 iso file to the `/mnt/iso` directory.
#### Limiting the Download Speed
To limit the download speed, use the `--limit-rate` option. By default, the speed is measured in bytes/second. Append `k` for kilobytes, `m` for megabytes, and `g` for gigabytes.
The following command will download the Go binary and limit the download speed to 1MB:
```
wget --limit-rate=1m https://dl.google.com/go/go1.10.3.linux-amd64.tar.gzCopy
```
This option is useful when you don’t want `wget` to consume all the available bandwidth.
#### Resuming a Download
You can resume a download using the `-c` option. This is useful if your connection drops during a download of a large file, and instead of starting the download from scratch, you can continue the previous one.
In the following example, we are resuming the download of the Ubuntu 18.04 iso file:
```
wget -c http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.isoCopy
```
If the remote server does not support resuming downloads, `wget` will start the download from the beginning and overwrite the existing file.
#### Downloading in Background
To download in the background, use the `-b` option. In the following example, we are downloading the OpenSuse iso file in the background:
```
wget -b https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Current.isoCopy
```
By default, the output is redirected to `wget-log` file in the current directory. To watch the status of the download, use the [`tail`](https://linuxize.com/post/linux-head-command/) command:
```
tail -f wget-logCopy
```
#### Changing the Wget User-Agent
Sometimes when downloading a file, the remote server may be set to block the Wget User-Agent. In situations like this, to emulate a different browser, pass the `-U` option.
```
wget --user-agent="Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" http://wget-forbidden.com/Copy
```
The command above will emulate Firefox 60 requesting the page from `wget-forbidden.com`
#### Downloading Multiple Files
If you want to download multiple files at once, use the `-i` option followed by the path to a local or external file containing a list of the URLs to be downloaded. Each URL needs to be on a separate line.
The following example shows how to download the Arch Linux, Debian, and Fedora iso files using the URLs specified in the `linux-distros.txt` file:
```
wget -i linux-distros.txtCopy
```
linux-distros.txt
```txt
http://mirrors.edge.kernel.org/archlinux/iso/2018.06.01/archlinux-2018.06.01-x86_64.iso
https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-9.4.0-amd64-netinst.iso
https://download.fedoraproject.org/pub/fedora/linux/releases/28/Server/x86_64/iso/Fedora-Server-dvd-x86_64-28-1.1.iso
```
Copy
If you specify `-` as a filename, URLs will be read from the standard input.
#### Downloading via FTP
To download a file from a password-protected FTP server, specify the username and password as shown below:
```
wget --ftp-user=FTP_USERNAME --ftp-password=FTP_PASSWORD ftp://ftp.example.com/filename.tar.gzCopy
```
#### Creating a Mirror of a Website
To create a mirror of a website with `wget`, use the `-m` option. This will create a complete local copy of the website by following and downloading all internal links as well as the website resources (JavaScript, CSS, Images).
```
wget -m https://example.comCopy
```
If you want to use the downloaded website for local browsing, you will need to pass a few extra arguments to the command above.
```
wget -m -k -p https://example.comCopy
```
The `-k` option will cause `wget` to convert the links in the downloaded documents to make them suitable for local viewing. The `-p` option will tell `wget` to download all necessary files for displaying the HTML page.
#### Skipping Certificate Check
If you want to download a file over HTTPS from a host that has an invalid SSL certificate, use the `--no-check-certificate` option:
```
wget --no-check-certificate https://domain-with-invalid-ss.comCopy
```
#### Downloading to the Standard Output
In the following example, `wget` will quietly ( flag `-q`) download and output the latest WordPress version to stdout ( flag `-O -`) and pipe it to the `tar` utility, which will extract the archive to the `/var/www` directory.
```
wget -q -O - "http://wordpress.org/latest.tar.gz" | tar -xzf - -C /var/wwwCopy
```
#### Conclusion
With `wget`, you can download multiple files, resume partial downloads, mirror websites, and combine the Wget options according to your needs.
To learn more about Wget, visit the [GNU wget Manual](https://www.gnu.org/software/wget/manual/wget.html) page.如果要学习Wget更多命令用法,请参考:https://www.gnu.org/software/wget/manual/wget.html
## yum命令
Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无须繁琐地一次次下载、安装。
```css
yum [选项] [参数]
```
### 选项
```php
-h, --help 显示此帮助消息并退出
-t, --tolerant 忽略错误
-C, --cacheonly 完全从系统缓存运行,不升级缓存
-c [config file], --config=[config file]
配置文件路径
-R [minutes], --randomwait=[minutes]
命令最长等待时间
-d [debug level], --debuglevel=[debug level]
调试输出级别
--showduplicates 在 list/search 命令下,显示源里重复的条目
-e [error level], --errorlevel=[error level]
错误输出级别
--rpmverbosity=[debug level name]
RPM 调试输出级别
-q, --quiet 静默执行
-v, --verbose 详尽的操作过程
-y, --assumeyes 回答全部问题为是
--assumeno 回答全部问题为否
--version 显示 Yum 版本然后退出
--installroot=[path] 设置安装根目录
--enablerepo=[repo] 启用一个或多个软件源(支持通配符)
--disablerepo=[repo] 禁用一个或多个软件源(支持通配符)
-x [package], --exclude=[package]
采用全名或通配符排除软件包
--disableexcludes=[repo]
禁止从主配置,从源或者从任何位置排除
--disableincludes=[repo]
disable includepkgs for a repo or for everything
--obsoletes 更新时处理软件包取代关系
--noplugins 禁用 Yum 插件
--nogpgcheck 禁用 GPG 签名检查
--disableplugin=[plugin]
禁用指定名称的插件
--enableplugin=[plugin]
启用指定名称的插件
--skip-broken 忽略存在依赖关系问题的软件包
--color=COLOR 配置是否使用颜色
--releasever=RELEASEVER
在 yum 配置和 repo 文件里设置 $releasever 的值
--downloadonly 仅下载而不更新
--downloaddir=DLDIR 指定一个其他文件夹用于保存软件包
--setopt=SETOPTS 设置任意配置和源选项
--bugfix Include bugfix relevant packages, in updates
--security Include security relevant packages, in updates
--advisory=ADVS, --advisories=ADVS
Include packages needed to fix the given advisory, in
updates
--bzs=BZS Include packages needed to fix the given BZ, in
updates
--cves=CVES Include packages needed to fix the given CVE, in
updates
--sec-severity=SEVS, --secseverity=SEVS
Include security relevant packages matching the
severity, in updates
```
## 参数
```dart
check 检查 RPM 数据库问题
check-update 检查是否有可用的软件包更新
clean 删除缓存数据
deplist 列出软件包的依赖关系
distribution-synchronization 已同步软件包到最新可用版本
downgrade 降级软件包
erase 从系统中移除一个或多个软件包
fs Acts on the filesystem data of the host, mainly for removing docs/lanuages for minimal hosts.
fssnapshot Creates filesystem snapshots, or lists/deletes current snapshots.
groups 显示或使用、组信息
help 显示用法提示
history 显示或使用事务历史
info 显示关于软件包或组的详细信息
install 向系统中安装一个或多个软件包
langavailable Check available languages
langinfo List languages information
langinstall Install appropriate language packs for a language
langlist List installed languages
langremove Remove installed language packs for a language
list 列出一个或一组软件包
load-transaction 从文件名中加载一个已存事务
makecache 创建元数据缓存
provides 查找提供指定内容的软件包
reinstall 覆盖安装软件包
repo-pkgs 将一个源当作一个软件包组,这样我们就可以一次性安装/移除全部软件包。
repolist 显示已配置的源
search 在软件包详细信息中搜索指定字符串
shell 运行交互式的 yum shell
swap Simple way to swap packages, instead of using shell
update 更新系统中的一个或多个软件包
update-minimal Works like upgrade, but goes to the 'newest' package match which fixes a problem that affects your system
updateinfo Acts on repository update information
upgrade 更新软件包同时考虑软件包取代关系
version 显示机器和/或可用的源版本。
```
## apt命令
Advanced Packaging Tool(apt)是[Linux](https://baike.baidu.com/item/Linux/27050)下的一款安装包[管理工具](https://baike.baidu.com/item/管理工具/9143974),是一个客户/[服务器系统](https://baike.baidu.com/item/服务器系统/2997788)。
apt-cache search # ——(package 搜索包)
apt-cache show #——(package 获取包的相关信息,如说明、大小、版本等)
sudo [apt-get](https://baike.baidu.com/item/apt-get) install # ——(package 安装包)
sudo apt-get reinstall # —–(package - - reinstall 重新安装包)
sudo apt-get -f install # —–(强制安装?#”-f = –fix-missing”当是修复安装吧…)
sudo [apt-get](https://baike.baidu.com/item/apt-get) remove #—–(package 删除包)
sudo apt-get remove --purge # ——(package 删除包,包括删除配置文件等)
sudo apt-get autoremove --purge # —-(package 删除包及其依赖的软件包配置文件等(只对6.10有效,强烈推荐)
sudo apt-get update #——更新源
sudo apt-get upgrade #——更新已安装的包
sudo apt-get dist-upgrade # ———升级系统
sudo [apt-get](https://baike.baidu.com/item/apt-get) dselect-upgrade #——使用 dselect 升级
apt-cache depends #——-(package 了解使用依赖)
apt-cache rdepends # ——(package 了解某个具体的依赖?#当是查看该包被哪些包依赖吧…)
sudo apt-get build-dep # ——(package 安装相关的编译环境)
apt-get source #——(package 下载该包的源代码)
sudo apt-get clean && sudo apt-get autoclean # ——–清理下载文件的存档 && 只清理过时的包
sudo [apt-get](https://baike.baidu.com/item/apt-get) check #——-检查是否有损坏的依赖
apt-get install# ——(下载要安装的包&所有依赖的包,同时进行包的安装或升级。如果某个包设置了 hold (停止标志,就会被搁在一边(即不会被升级))。更多 hold 细节请看下面。)
apt-get remove [--purge]# ——(移除某个包以及任何依赖这个包的其它包。)
--purge 指明这个包应该被完全清除 (purged) ,更多信息请看 dpkg -P。
apt-get update# ——(升级来自 Debian 镜像的包列表,如果你想安装当天的任何软件,至少每天运行一次,而且每次修改了/etc/apt/sources.list 后,必须执行。)
apt-get upgrade [-u]# ——升级所有已经安装的包为最新可用版本。不会安装新的或移除老的包。形前端(其中一些在使用前得先安装)。这里 dselect 无疑是最强大的,也是最古老,最难驾驭。
Httpie安装及使用