使用python3 http.server在本地进行测试
这也是一种有趣的测试方式。在一个 shell 上,启动一个本地文件服务器:
python3 -m http.server 8000
然后在第二个 shell 上发出请求:
printf 'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n' | nc localhost 8000
Host: 标头在 HTTP 1.1 中是必需的。
这显示了目录的 HTML 列表,就像您从以下位置看到的一样:
firefox http://localhost:8000
接下来你可以尝试列出文件和目录并观察响应:
printf 'GET /my-subdir/ HTTP/1.1\n\n' | nc localhost 8000
printf 'GET /my-file HTTP/1.1\n\n' | nc localhost 8000
每次您发出成功的请求时,服务器都会打印:
127.0.0.1 - - [05/Oct/2018 11:20:55] "GET / HTTP/1.1" 200 -
确认已收到。
example.com
这个IANA 维护的域是另一个很好的测试 URL:
printf 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n' | nc example.com 80
并比较:http://example.com/
httpsSSL
nc 似乎无法处理https URL。相反,您可以使用:
sudo apt-get install nmap
printf 'GET / HTTP/1.1\r\nHost: github.com\r\n\r\n' | ncat --ssl github.com 443
另见:https://serverfault.com/questions/102032/connecting-to-https-with-netcat-nc/650189#650189
如果您尝试nc,它只会挂起:
printf 'GET / HTTP/1.1\r\nHost: github.com\r\n\r\n' | nc github.com 443
并尝试端口80:
printf 'GET / HTTP/1.1\r\nHost: github.com\r\n\r\n' | nc github.com 443
只是给https版本一个重定向响应:
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://github.com/
Connection: keep-alive
在 Ubuntu 18.04 上测试。