Tiny Web Server Tiny Web服务器练习

本文通过使用《深入理解计算机组成》一书中的Web编程实例,详细介绍了如何利用Telnet登录服务器并发送HTTP请求来获取HTML文件的过程。同时展示了如何请求动态内容及服务器端的响应机制。

 使用《深入理解计算机组成》书里面的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

转载于:https://www.cnblogs.com/yanhc/archive/2010/03/19/2175256.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值