telnet 和 nc

telnet

macOS 10.13 去掉了telnet。
安装, brew install telnet
简单使用示例,

1
2
3
4
$ telnet www.baidu.com 80
Trying 61.135.169.121...
Connected to www.a.shifen.com.
Escape character is '^]'.

输入

1
2
GET / HTTP/1.1
Host: www.baidu.com

Response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 14615
Content-Type: text/html
Date: Mon, 18 Jun 2018 16:01:48 GMT
Last-Modified: Mon, 11 Jun 2018 11:19:00 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Pragma: no-cache
Server: BWS/1.1
Set-Cookie: BAIDUID=47DF48A1AE3963EA34DF3B898FF8AAC3:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=47DF48A1AE3963EA34DF3B898FF8AAC3; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1529337708; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1

<!DOCTYPE html><!--STATUS OK-->
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<link rel="dns-prefetch" href="//s1.bdstatic.com"/>
<link rel="dns-prefetch" href="//t1.baidu.com"/>
<link rel="dns-prefetch" href="//t2.baidu.com"/>
<link rel="dns-prefetch" href="//t3.baidu.com"/>
<link rel="dns-prefetch" href="//t10.baidu.com"/>
<link rel="dns-prefetch" href="//t11.baidu.com"/>
<link rel="dns-prefetch" href="//t12.baidu.com"/>
<link rel="dns-prefetch" href="//b1.bdstatic.com"/>
<title>百度一下,你就知道</title>
(以下省略)
……
……
……

nc

即:netcat

1
$ nc www.baidu.com 80

输入

1
2
GET / HTTP/1.1
Host: www.baidu.com

Response也是一个网页。


上述示例中的以下输入,

1
2
GET / HTTP/1.1
Host: www.baidu.com

和浏览器访问时的Request是一样的,可以用浏览器访问网站,Chrome开发者工具 - Network - Headers - Request Headers - view source