使用python3 http.server
在本地测试它
这也是一个有趣的测试方法。在一个终端窗口中,启动一个本地文件服务器:
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/
https
SSL
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上进行了测试。
curl
或wget
来完成这种事情。 - ki9