后台小程序开发的全方位指南
746
2022-11-01
httpcat一个用于在命令行上构造原始HTTP请求的简单实用程序
httpcat
httpcat is a simple utility for constructing raw HTTP requests on the command line.
Why?
Sometimes it is useful to be able to create an actual raw HTTP request on the command line:
To debug a server issueTo test the handling of invalid HTTP requestsTo learn how HTTP works under the hood
In such cases, existing CLI HTTP clients—such as httpie, curl, or wget —are too high-level as they provide an abstraction layer and one doesn't have a complete control over the exact raw data that gets written to the HTTP socket connection.
Lower-level tools, such as the popular netcat, are better suited for this job.
However, the syntax of HTTP requires headers to be separated with \r\n which makes it difficult to produce them on the command line. A typical solution involves the use of echo:
$ echo -ne 'GET / HTTP/1.1\r\nHost: httpbin.org\r\nContent-Length: 5\r\n\r\nHello' | \ nc localhost 8000
httpcat makes this easier:
How it works
Reads command arguments as lines and then lines from stdinAuto-completes them, if necessaryWrites them to stdout
Features
Automatic \r\n completionAutomatic Method completion in Request-LineAutomatic HTTP-Version completion in Request-Line
Usage
Interactively create a request and send it with nc:
$ httpcat -v | nc httpbin.org 80POST /post HTTP/1.1> POST /post HTTP/1.1\r\nHost: httpbin.org> Host: httpbin.org\r\nContent-Length: 6> Content-Length: 6\r\n> \r\nHello> Hello
Specify the whole request in the arguments. Here '' represents an empty line which will be converted to \r\n\ separating the headers and the body. Note also that the omitted HTTP-Version is auto-completed:
$ httpcat -v 'POST /post' 'Host: httpbin.org' 'Content-Length: 5' '' 'Hello' | nc httpbin.org 80> POST /post HTTP/1.1\r\n> Host: httpbin.org\r\n> Content-Length: 5\r\n> \r\n> Hello
Omitted Method is set to GET and HTTP-Version is auto-completed:
$ httpcat -v / 'Host: example.org' '' | nc example.org 80> GET / HTTP/1.1\r\n> Host: example.org\r\n> \r\n
You can, for example, use stdin for data and arguments for headers:
$ cat file.txt | httpcat -v 'POST /post' 'Host: httpbin.org' 'Content-Length: 16' '' | nc httpbin.org 80> POST /post HTTP/1.1\r\n> Host: httpbin.org\r\n> Content-Length: 16\r\n> \r\n> Hello from file
See also httpcat --help:
usage: httpcat [-h] [-V, --version] [-v] [-n] [line [line ...]]Create raw HTTP requests on the command line.positional arguments: line input lines read before lines from stdinoptional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -v, --verbose print info about output lines to stderr -n, --no-stdin disable reading of lines from stdin
Dependencies
python 3
Installation
pip3 install httpcat
Alternatively, you can just download httpcat.py manually and invoke it as ./httpcat.py instead of httpcat.
Tests
python3 setup.py test
Changelog
0.0.2 (2016-12-13)Added -v, --verbose and the command is more quiet by default.Added -n, --no-stdinAdded -h, --helpAdded -V, --version 0.0.1 (2016-12-12)Initial release.
Contact
Jakub Roztocil
https://github.com/jakubroztocilhttps://twitter.com/jakubroztocilhttp://roztocil.co
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~