Use Python socket to implement a simple http server (difference between post and get)

Source: Internet
Author: User
Tags webp

Prerequisites:

For more information about the http protocol, see here.

For socket basic functions, see here.

For python network programming basics, refer to here.


To put it bluntly, we have previously implemented the use of linux c or python as a client to obtain http responses, and also implemented a simple http server using the muduo library, now to implement a python version of simple http server, code adapted from the 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/env python
# Coding = UTF-8

Import socket
Import re

HOST =''
PORT = 8000

# Read index.html, put into HTTP response data
Index_content = '''
HTTP/1.x 200 OK
Content-Type: text/html

'''

File = open('index.html ', 'R ')
Index_content + = file. read ()
File. close ()

# Read reg.html, put into HTTP response data
Reg_content = '''
HTTP/1.x 200 OK
Content-Type: text/html

'''

File = open('reg.html ', 'R ')
Reg_content + = file. read ()
File. close ()

# Read picture, put into HTTP response data
File = open ('HTTP: // www.bkjia.com/uploads/allianz 140530/00%3ez-0.jpg', 'rb ')
Pic_content = '''
HTTP/1.x 200 OK
Content-Type: image/jpg

'''
Pic_content + = file. read ()
File. close ()



# Configure socket
Sock = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Sock. bind (HOST, PORT ))
Sock. listen (100)

# Infinite loop
While True:
# Maximum number of requests waiting
Conn, addr = sock. accept ()
Request = conn. recv (1024)
Method = request. split ('') [0]
Src = request. split ('') [1]

Print 'connect by: ', addr
Print 'request is: \ n', Request

# Deal wiht GET method
If method = 'get ':
If src = '/index.html ':
Content = index_content
Elif src = '/http://www.bkjia.com/uploads/allianz 140530/00%3ez-0.jpg ':
Content = pic_content
Elif src = '/reg.html ':
Content = reg_content
Elif re. match ('^ /\?. * $ ', Src ):
Entry = src. split ('? ') [1] # main content of the request
Content = 'HTTP/1.x 200 OK \ r \ nContent-Type: text/html \ r \ n \ r \ N'
Content + = entry
Content + ='
Register successs!

'
Else:
Continue


# Deal with POST method
Elif method = 'post ':
Form = request. split ('\ r \ n ')
Entry = form [-1] # main content of the request
Content = 'HTTP/1.x 200 OK \ r \ nContent-Type: text/html \ r \ n \ r \ N'
Content + = entry
Content + ='
Register successs!

'

######
# More operations, such as put the form into database
#...
######

Else:
Continue

Conn. sendall (content)

# Close connection
Conn. close ()

Chmod + x httpServer. py and run./httpServer. py

Use a browser as a client to access the server

The httpServer. py directory contains index.html, reg.html, http://www.bkjia.com/uploads/allimg/140530/004403EZ-0.jpg


1. Access the Directory: http: // 192.168.56.188: 8000/index.html

Server output:

Connect by: ('192. 168.56.1 ', 192)
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


The response code informs the customer that the response is "index.html". The index.html is as follows:

HTML Code
1
2
3
4
5
6
7
8
9
10


Jinan University



Python HTTP Server





Further access to the ingress:

Connect by: ('192. 168.56.1 ', 192)
Request is:
GET/http://www.bkjia.com/uploads/allimg/140530/004403EZ-0.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


Similarly, the server responds to the binary data of the header + image, as shown in:


Of course you can also directly access http: // 192.168.56.188: 8000/http://www.bkjia.com/uploads/allimg/140530/004403EZ-0.jpg


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + MqGit8POysS/samples/s7xxvfK5LP2o7o8L3A + CjxwPkNvbm5lY3QgYnk6ICAo "192.168.56.1 ', 6282)
Request is:
GET/reg.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


In the same way, we can echo the header "reg.html". The Registry form is as follows:

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





Register page








Enter some information and click submit data, for example:


In this case, the browser accesses http: // 192.168.56.188: 8000/

The server output is:

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


Note: name = value in the form, separated.


Looking back at the code, we just sent the data body submitted by the browser back directly, and then output register success! The browser output is as follows:



If we put

What will happen if the method is changed to get?


In this case, the browser will access http: // 192.168.56.188: 8000 /? User = simba & psw = 1990 & repsw = 1990 & sex = nan & tech = java & country = cn


The server output is:

Connect by: ('192. 168.56.1 ', 192)
Request is:
GET /? User = simba & psw = 1990 & repsw = 1990 & sex = nan & tech = java & country = cn 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
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


Because the returned data is consistent with the post data, the output displayed by the browser is the same.


Here we can summarize the differences between post and get submission:

Get is submitted. All submitted information is displayed in the address bar. Sensitive data is insecure. Large data volumes cannot be submitted due to limited storage space in the address bar. The information is encapsulated in the request line of the request message.

And the post submission encapsulates the information in the Request body.



Refer:

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

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.