1 个不稳定版本
0.1.0 | 2024年5月7日 |
---|
#281 在 HTTP服务器
8KB
59 代码行
HTTT
一个最小的恶意HTTP服务器,通过均匀间隔响应。
这对认为请求超时的HTTP客户端来说是不利的,因为底层TCP读取超时。例如,Python的requests
模块。
安装
cargo install --git https://github.com/aalekhpatel07/httt.git
使用
- 在端口
9000
上启动一个HTTP服务器,该服务器一次发送一个字节,交替写入和休眠1
秒。
httt --interval 1 --port 9000
测试
- 首先,在端口9000上启动一个每200ms发送一个字节的服务器(即发送所有19个字节需要大约
3.8
秒)
httt --interval 0.2 --port 9000
- 然后,打开一个已安装
requests
的Python shell。并观察requests.get
中的timeout
kwarg并不意味着请求超时
>>> import requests
>>> import time
>>>
>>> def time_it():
... start = time.perf_counter_ns()
... response = requests.get("https://127.0.0.1:9000", timeout=1)
...
... # Notice how even though `timeout=1` suggests the request
... # will timeout if it takes longer than 1 second, but it actually takes
... # around 3 seconds before completing and it is allowed to run uninterrupted.
...
... end = time.perf_counter_ns()
... print("request took: {(end - start)/1e9:.3f} seconds")
...
>>> time_it()
request took: 3.823 seconds
- 如果相反,你用3秒超时
curl
服务器,则它会提前关闭连接,正如预期的那样
$ curl -v -m 3 https://127.0.0.1:9000
* Trying 127.0.0.1:9000...
* Connected to localhost (127.0.0.1) port 9000 (#0)
> GET / HTTP/1.1
> Host: localhost:9000
> User-Agent: curl/8.0.1
> Accept: */*
>
* Operation timed out after 3000 milliseconds with 0 bytes received
* Closing connection 0
curl: (28) Operation timed out after 3000 milliseconds with 0 bytes received
依赖项
~3.5–10MB
~90K SLoC