如何使用 netcat 手动发出 HTTP GET 请求?
How to make an HTTP GET request manually with netcat?
所以,我必须从 http://www.rssweather.com/dir/Asia/India 的任何一个城市检索温度。
假设我想检索 Kanpur 的。
如何使用 Netcat 发出 HTTP GET 请求?
我正在做这样的事情。
nc -v rssweather.com 80
GET http://www.rssweather.com/wx/in/kanpur/wx.php HTTP/1.1
我什至不知道我的方向是否正确。我找不到任何关于如何使用 netcat 发出 HTTP get 请求的好教程,所以我把它贴在这里。
当然你可以在搜索 google 的标准中挖掘,但实际上如果你只想得到一个 URL,那是不值得的。
您还可以在端口上以侦听模式启动 netcat:
nc -l 64738
(有时 nc -l -p 64738
是正确的参数列表)
...然后使用真实浏览器向该端口发送浏览器请求。只需在浏览器中输入 http://localhost:64738
即可查看。
在您的实际情况下,问题是 HTTP/1.1 不会自动关闭连接,而是等待您要检索的下一个 URL。解决方法很简单:
使用HTTP/1.0:
GET /this/url/you/want/to/get HTTP/1.0
Host: www.rssweather.com
<empty line>
或使用 Connection:
请求 header 说出您之后要关闭的服务器:
GET /this/url/you/want/to/get HTTP/1.1
Host: www.rssweather.com
Connection: close
<empty line>
Extension: GET header 后只写请求的路径部分。您要从中获取数据的主机名属于 Host:
header,如您在我的示例中所见。这是因为多个网站可以 运行 在同一个网络服务器上,所以浏览器需要告诉他,它想从哪个网站加载页面。
这对我有用:
$ nc www.rssweather.com 80
GET /wx/in/kanpur/wx.php HTTP/1.0
Host: www.rssweather.com
然后点击两次 <enter>
,即一次用于远程 http 服务器,一次用于 nc
命令。
在 MacOS 上,您需要如下 -c 标志:
Little-Net:~ minfrin$ nc -c rssweather.com 80
GET /wx/in/kanpur/wx.php HTTP/1.1
Host: rssweather.com
Connection: close
[empty line]
响应如下:
HTTP/1.1 200 OK
Date: Thu, 23 Aug 2018 13:20:49 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
-c 标志描述为 "Send CRLF as line-ending"。
要符合 HTTP/1.1,您需要主机 header,如果您想禁用保活,则需要 "Connection: close"。
使用 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
HTTP 1.1 要求 Host:
header。
这显示了目录的 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
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.
你甚至不需要 use/install netcat
- 通过未使用的文件描述符创建一个 tcp 套接字,即我在这里使用 88
- 把请求写进去
使用 fd
exec 88<>/dev/tcp/rssweather.com/80
echo -e "GET /dir/Asia/India HTTP/1.1\nhost: www.rssweather.com\nConnection: close\n\n" >&88
sed 's/<[^>]*>/ /g' <&88
所以,我必须从 http://www.rssweather.com/dir/Asia/India 的任何一个城市检索温度。
假设我想检索 Kanpur 的。
如何使用 Netcat 发出 HTTP GET 请求?
我正在做这样的事情。
nc -v rssweather.com 80
GET http://www.rssweather.com/wx/in/kanpur/wx.php HTTP/1.1
我什至不知道我的方向是否正确。我找不到任何关于如何使用 netcat 发出 HTTP get 请求的好教程,所以我把它贴在这里。
当然你可以在搜索 google 的标准中挖掘,但实际上如果你只想得到一个 URL,那是不值得的。
您还可以在端口上以侦听模式启动 netcat:
nc -l 64738
(有时 nc -l -p 64738
是正确的参数列表)
...然后使用真实浏览器向该端口发送浏览器请求。只需在浏览器中输入 http://localhost:64738
即可查看。
在您的实际情况下,问题是 HTTP/1.1 不会自动关闭连接,而是等待您要检索的下一个 URL。解决方法很简单:
使用HTTP/1.0:
GET /this/url/you/want/to/get HTTP/1.0
Host: www.rssweather.com
<empty line>
或使用 Connection:
请求 header 说出您之后要关闭的服务器:
GET /this/url/you/want/to/get HTTP/1.1
Host: www.rssweather.com
Connection: close
<empty line>
Extension: GET header 后只写请求的路径部分。您要从中获取数据的主机名属于 Host:
header,如您在我的示例中所见。这是因为多个网站可以 运行 在同一个网络服务器上,所以浏览器需要告诉他,它想从哪个网站加载页面。
这对我有用:
$ nc www.rssweather.com 80
GET /wx/in/kanpur/wx.php HTTP/1.0
Host: www.rssweather.com
然后点击两次 <enter>
,即一次用于远程 http 服务器,一次用于 nc
命令。
在 MacOS 上,您需要如下 -c 标志:
Little-Net:~ minfrin$ nc -c rssweather.com 80
GET /wx/in/kanpur/wx.php HTTP/1.1
Host: rssweather.com
Connection: close
[empty line]
响应如下:
HTTP/1.1 200 OK
Date: Thu, 23 Aug 2018 13:20:49 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
-c 标志描述为 "Send CRLF as line-ending"。
要符合 HTTP/1.1,您需要主机 header,如果您想禁用保活,则需要 "Connection: close"。
使用 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
HTTP 1.1 要求 Host:
header。
这显示了目录的 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
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.
你甚至不需要 use/install netcat
- 通过未使用的文件描述符创建一个 tcp 套接字,即我在这里使用 88
- 把请求写进去
使用 fd
exec 88<>/dev/tcp/rssweather.com/80 echo -e "GET /dir/Asia/India HTTP/1.1\nhost: www.rssweather.com\nConnection: close\n\n" >&88 sed 's/<[^>]*>/ /g' <&88