简单快速http echo服务器
hecho的Python项目详细描述
hecho是一个快速简单的http回声服务器。
一个例子在“http://hecho.bigode.net/”上运行。
它使用Falconhttp库和Bjoernwsgi服务器为成千上万的用户提供服务 每秒只使用一个CPU内核和大约15MB RAM的请求。
响应是包含请求的“美化”json。
简单的请求/响应示例:
$ curl --header "Content-Type: application/json" \ --request POST --data '{"user": "john", "pass": "1234"}' \ http://localhost:8000?p=param { "headers": { "ACCEPT": "*/*", "CONTENT-LENGTH": "32", "CONTENT-TYPE": "application/json", "HOST": "localhost:8000", "USER-AGENT": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" }, "json": { "pass": "1234", "user": "john" }, "method": "POST", "origin": "127.0.0.1", "params": { "p": "param" }, "url": "http://localhost:8000/?p=param" }
它支持get、post、put、patch和delete方法。
请求数据必须是json或x-www-form-urlencoded(标准的简单post)。
安装
使用pip安装:
pip install hecho
用法
Usage: hecho [options] start|stop|restart simple and fast http echo server Options: --version show program's version number and exit -h, --help show this help message and exit -l ADDRESS address to listen (default: localhost) -p PORT application port (default: 8000) --log-path=LOGPATH logfile path (default: ~/.hecho/hecho.log) --pid-path=PIDPATH pidfile path (default: ~/.hecho/hecho.pid) --foreground run in foreground mode (default: disabled) --disable-log disable logging (default: disabled)
运行
启动服务器:
$ hecho start
验证日志、进程和TCP侦听地址:
$ tail ~/.hecho/hecho.log [28/Dec/2018 16:23:10] Starting server... $ ps aux | grep hecho myuser 23648 0.0 1.5 214444 15720 ? S 15:54 0:00 python hecho.py start $ netstat -nlp | grep python tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 23648/python
简单测试:
$ curl localhost:8000 { "headers": { "ACCEPT": "*/*", "HOST": "localhost:8000", "USER-AGENT": "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" }, "method": "GET", "origin": "127.0.0.1", "params": {}, "url": "http://localhost:8000/" }
验证日志:
$ tail ~/.hecho/hecho.log [28/Dec/2018 16:23:10] Starting server... [28/Dec/2018 16:24:55] 127.0.0.1 GET http://localhost:8000/ 200 313 curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
停止服务器:
$ hecho stop
注释
- 适用于Python2.7和Python3.4+