我正在尝试创建mock服务器。我当然可以自己编写代码,但我发现ncat
理论上可以用于此。这应该比我的新代码更多的用户可以使用。。。但是:
如果我开始";服务器";像这样:
ncat -v -l -p 5555 -c 'while true; do read i && echo [echo] $i; done'
并发出这样的请求:
curl localhost:5555 -X POST --data-binary 'test'
我得到这个:
curl: (1) Received HTTP/0.9 when not allowed
ncat版本为7.80。为什么我得到的http版本这么旧?这是正常的还是我这边的问题?如何修复?我绝对不能调整卷曲部分来接受过时的回复。
如果这是不可能的,有人能推荐现有的工作方式来代替使用nc来监听传入的请求,并对每个请求应用一些命令吗?
这与netcat的响应有关。它不是有效的HTTP/1.x(请参阅https://www.w3.org/Protocols/HTTP/1.0/spec.html#Response)。
下面是一个通过文件的HTTP/1.1响应示例。
$ cat index.html
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: netcat!
<!doctype html>
<html><body><h1>A webpage served by netcat</h1></body></html>
现在通过netcat返回。
~$ cat index.html | sudo nc -q0 -l 80
默认情况下,Curl正在工作(使用http/1.1(。
% curl -v 34.145.32.6
* Trying 34.145.32.6:80...
* Connected to 34.145.32.6 (34.145.32.6) port 80 (#0)
> GET / HTTP/1.1
> Host: 34.145.32.6
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=UTF-8
< Server: netcat!
* no chunk, no close, no size. Assume close to signal end
<
<!doctype html>
<html><body><h1>A webpage served by netcat</h1></body></html>
* Closing connection 0
注意像HTTP/1.1 200 OK
和Content-Type: text/html; charset=UTF-8
这样的东西。
如果您想在无效的HTTP服务器响应中使用curl,请尝试在curl中使用telnet协议(而不是HTTP(。
% curl -v telnet://34.145.32.6:80
* Trying 34.145.32.6:80...
* Connected to 34.145.32.6 (34.145.32.6) port 80 (#0)
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: netcat!
<!doctype html>
<html><body><h1>A webpage served by netcat</h1></body></html>
* Closing connection 0
当我使用可能不同的nc/netcat版本;我不认为这是你问题的根源。