Tiny Web Server Tiny Web服务器练习

 使用《深入理解计算机组成》书里面的Web编程里的代码,了解HTTP和HTML和CGI的原理

********************************************** TELNET ********************************************** vadmin@vadmin:~$ telnet localhost 1000 Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET / HTTP/1.1 HTTP/1.0 200 OK Server: Tiny Web Server Content-length: 112 Content-type: text/html <html> <head> <title>Google</title> </head> <body> <p>Hi, <p>this is the <B>home.html</B> file. </body> </html> Connection closed by foreign host. vadmin@vadmin:~$ ******************************************* SERVER ******************************************* ^C root@vadmin:/home/vadmin/york/code/netp/tiny# ./tiny 1000 ***buf: GET / HTTP/1.1 end of buf*** ***method:GET; uri:/; version:HTTP/1.1*** ***************************** ***hdrs: end of hdrs*** ***************************** uri:/; filename:./home.html; cgiargs:. ***HTTP response heders: HTTP/1.0 200 OK Server: Tiny Web Server Content-length: 112 Content-type: text/html end response*** ***file: <html> <head> <title>Google</title> </head> <body> <p>Hi, <p>this is the <B>home.html</B> file. </body> </html> end of file***   

使用Telnet可以登录服务器,并且,直接使用发送HTTP请求

GET / HTTP/1.1

 

后面是一个空行,说明HTTP request header结束

请求uri是/,方法为GET,请求home.html

服务器,会响应HTTP response header

HTTP/1.0 200 OK
Server: Tiny Web Server
Content-length: 112
Content-type: text/html

 

最后一个空行表示HTTP response header结束,

先显示版本,然后是状态码200,代表成功OK

然后是一些其他的HTTP response header

最后空行代表HTTP response header结束

 

接下来是HTTP response body,即一个html文件。

<html>
<head>
<title>Google</title>
</head>
<body>
<p>Hi, <p>this is the <B>home.html</B> file.
</body>
</html>

如果使用浏览器的话,就可以在网页上显示内容了,

比如在浏览器输入

http://localhost:1000/

浏览器会向服务器发送上面的

GET / HTTP/1.1

下面的就一样了,最后,服务器吧html文件传送过来,浏览器就把html解析了,然后显示出来。

 

照这么看,貌似自己都可以写一个简单的浏览器了,呵呵~~~~

 

当然,也可以请求一些图片什么的

比如

GET /logo.gif HTTP/1.1

当然得要求服务器目录下面有这个文件,不然就会报错

404,not find

 

还可以请求动态内容,比如

*********************************************** TELNET *********************************************** vadmin@vadmin:~$ telnet localhost 1000 Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GET /cgi-bin/adder?1&3 HTTP/1.1 HTTP/1.0 200 OK Server: Tiny Web Server Content-length: 105 Content-type: text/html Welcome to add.com: THE Internet addition portal. <p>The answer is: 1 + 3 = 4 <p>Thanks for visiting! Connection closed by foreign host. vadmin@vadmin:~$ *********************************************** SERVER *********************************************** root@vadmin:/home/vadmin/york/code/netp/tiny# ./tiny 1000 ***buf: GET /cgi-bin/adder?1&3 HTTP/1.1 end of buf*** ***method:GET; uri:/cgi-bin/adder?1&3; version:HTTP/1.1*** ***************************** ***hdrs: end of hdrs*** ***************************** uri:/cgi-bin/adder; filename:./cgi-bin/adder; cgiargs:1&3. ***HTTP response header: HTTP/1.0 200 OK Server: Tiny Web Server end of HTTP response header***

GET /cgi-bin/adder?1&3 HTTP/1.1

服务器会把参数读取,并求出结果,通过HTTP发送过来html文件。

参数格式为app?var1&var2

你可能感兴趣的:(Tiny Web Server Tiny Web服务器练习)