用Python socket实现一个简单的http服务器(post 与get 的区别),,预备知识:关于htt


预备知识:

关于http协议的基础请参考这里。

关于socket基础函数请参考这里。

关于python网络编程基础请参考这里。


废话不多说,前面实现过使用linux c 或者python 充当客户端来获取http 响应,也利用muduo库实现过一个简易http服务器,现在来实现一个python版的简易http服务器,代码改编自http://www.cnblogs.com/vamei/


httpServer.py

Python Code
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/envpython
#coding=utf-8

importsocket
importre

HOST=‘‘
PORT=8000

#Readindex.html,putintoHTTPresponsedata
index_content=‘‘‘
HTTP/1.x200ok
Content-Type:text/html

‘‘‘

file=open(‘index.html‘,‘r‘)
index_content+=file.read()
file.close()

#Readreg.html,putintoHTTPresponsedata
reg_content=‘‘‘
HTTP/1.x200ok
Content-Type:text/html

‘‘‘

file=open(‘reg.html‘,‘r‘)
reg_content+=file.read()
file.close()

#Readpicture,putintoHTTPresponsedata
file=open(‘T-mac.jpg‘,‘rb‘)
pic_content=‘‘‘
HTTP/1.x200ok
Content-Type:image/jpg

‘‘‘
pic_content+=file.read()
file.close()



#Configuresocket
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((HOST,PORT))
sock.listen(100)

#infiniteloop
whileTrue:
#maximumnumberofrequestswaiting
conn,addr=sock.accept()
request=conn.recv(1024)
method=request.split(‘‘)[0]
src=request.split(‘‘)[1]

print‘Connectby:‘,addr
print‘Requestis:\n‘,request

#dealwihtGETmethod
ifmethod==‘GET‘:
ifsrc==‘/index.html‘:
content=index_content
elifsrc==‘/T-mac.jpg‘:
content=pic_content
elifsrc==‘/reg.html‘:
content=reg_content
elifre.match(‘^/\?.*$‘,src):
entry=src.split(‘?‘)[1]#maincontentoftherequest
content=‘HTTP/1.x200ok\r\nContent-Type:text/html\r\n\r\n‘
content+=entry
content+=‘<br/><fontcolor="green"size="7">registersuccesss!</p>‘
else:
continue


#dealwithPOSTmethod
elifmethod==‘POST‘:
form=request.split(‘\r\n‘)
entry=form[-1]#maincontentoftherequest
content=‘HTTP/1.x200ok\r\nContent-Type:text/html\r\n\r\n‘
content+=entry
content+=‘<br/><fontcolor="green"size="7">registersuccesss!</p>‘

######
#Moreoperations,suchasputtheformintodatabase
#...
######

else:
continue

conn.sendall(content)

#closeconnection
conn.close()

chmod +x httpServer.py, 并运行./httpServer.py

使用浏览器当做客户端访问服务器

在httpServer.py 所在目录有index.html, reg.html, T-mac.jpg


1、访问目录: http://192.168.56.188:8000/index.html

服务器输出:

Connect by: (‘192.168.56.1‘, 6274)
Request is:
GET /index.html HTTP/1.1
Host: 192.168.56.188:8000
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/33.0.1750.146 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4


回顾代码可知我们给客户端的响应是头部+index.html, index.html如下:

HTML Code
1
2
3
4
5
6
7
8
9
10
<html>
<head>
<title>JinanUniversity</title>
</head>

<body>
<p>PythonHTTPServer</p>
<imgsrc="T-mac.jpg"/>
</body>
</html>

进而进一步访问T-mac.jpg,由于我们在实现服务器时使用短连接,即响应一次就关掉连接,所以客户端会再发起一次连接,如下:

Connect by: (‘192.168.56.1‘, 6275)
Request is:
GET /T-mac.jpg HTTP/1.1
Host: 192.168.56.188:8000
Connection: keep-alive
Accept: image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/33.0.1750.146 Safari/537.36
Referer: http://192.168.56.188:8000/index.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4


同样地,服务器响应头部+图片的二进制数据,如下图所示:

bubuko.com,布布扣

当然你也可以直接访问http://192.168.56.188:8000/T-mac.jpg


2、访问目录:http://192.168.56.188:8000/reg.html

服务器输出:

Connect by: (‘192.168.56.1‘, 6282)
Request is:
GET /reg.htmlHTTP/1.1
Host: 192.168.56.188:8000
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/33.0.1750.146 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4


同样地,我们把头部+reg.html 响应过去,reg.html 是注册表单如下:

HTML Code
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=GBK">
<!--metahttp-equiv="refresh"content="3;url=http://www.sina.com.cn"/-->
<title>registerpage</title>
</head>
<body>

<formaction="http://192.168.56.188:8000"method="post">
<tableborder="1"bordercolor="#0000ff"cellpadding=10cellspacing=0width=600>
<tr>
<thcolspan="2">注册表单</th>
</tr>
<tr>
<td>用户名称:</td>
<td><inputtype="text"name="user"/></td>
</tr>
<tr>
<td>输入密码:</td>
<td><inputtype="password"name="psw"/></td>
</tr>
<tr>
<td>确认密码:</td>
<td><inputtype="password"name="repsw"/></td>
</tr>
<tr>
<td>选择性别:</td>
<td>
<inputtype="radio"name="sex"value="nan"/>男
<inputtype="radio"name="sex"value="nv"/>女
</td>
</tr>
<tr>
<td>选择技术:</td>
<td>
<inputtype="checkbox"name="tech"value="java"/>JAVA
<inputtype="checkbox"name="tech"value="html"/>HTML
<inputtype="checkbox"name="tech"value="css"/>CSS
</td>
</tr>
<tr>
<td>选择国家:</td>
<td>
<selectname="country">
<optionvalue="none">--选择国家--</option>
<optionvalue="usa">--美国--</option>
<optionvalue="en">--英国--</option>
<optionvalue="cn">--中国--</option>
</select>
</td>
</tr>
<tr>
<thcolspan="2">
<inputtype="reset"value="清除数据"/>
<inputtype="submit"value="提交数据"/>
</th>
</tr>
</table>

</form>


</body>
</html>

我们随便填一些信息上去然后点击提交数据,如下图:

bubuko.com,布布扣

此时浏览器会访问http://192.168.56.188:8000/

服务器输出为:

Connect by: (‘192.168.56.1‘, 6578)
Request is:
POST / HTTP/1.1
Host: 192.168.56.188:8000
Connection: keep-alive
Content-Length: 59
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://192.168.56.188:8000
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/33.0.1750.146 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://192.168.56.188:8000/reg.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4

user=simba&psw=1990&repsw=1990&sex=nan&tech=java&country=cn


注意:即表单中的name=value,以&分隔。


回顾代码,我们只是将浏览器提交的数据体直接发回去,再输出register success! 浏览器输出如下图:

bubuko.com,布布扣


如果我们把 表单中的 <formaction="http://192.168.56.188:8000"method="post"> method 改成get,会是怎样的呢?


此时浏览器会访问 http://192.168.56.188:8000/?user=simba&psw=1990&repsw=1990&sex=nan&tech=java&country=cn


服务器输出为:

Connect by: (‘192.168.56.1‘, 6382)
Request is:
GET /?user=simba&psw=1990&repsw=1990&sex=nan&tech=java&country=cnHTTP/1.1
Host: 192.168.56.188:8000
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/33.0.1750.146 Safari/537.36
Referer: http://192.168.56.188:8000/reg.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4


因为我们应答回去的数据跟post一致,故浏览器看到的输出也是一样的。


在这里可以总结一下post 跟 get 提交的一些区别:

get提交,提交的信息都显示在地址栏中;对于敏感数据不安全;由于地址栏存储体积有限而不能提交大容量数据;将信息封装到了请求消息的请求行

中,而post 提交将信息封装到了请求体中。



参考:

http://www.cnblogs.com/vamei/archive/2012/10/30/2744955.html

http://www.cnblogs.com/vamei/archive/2012/10/31/2747885.html


用Python socket实现一个简单的http服务器(post 与get 的区别),布布扣,bubuko.com

用Python socket实现一个简单的http服务器(post 与get 的区别)

评论关闭