1.http Overview
Current version: 1.1
http protocol is a standard TCP client and server request response, such as the browser as a client sends a request to a server designated port, the server content back to the server
2. Protocol format
http define the client and server data exchange protocol
request the client sends the request message to the server, includes a request line (request url blank spaces method carriage return linefeed protocol version), the request header (key: value carriage return line feed key: value 2 * carriage return linefeed ), request data
#### a request request (get request data is not carried in the URL)
B ' the GET /favicon.ico the HTTP / 1.1 \ R & lt \ NHOst: 127.0.0.1:8888\r\nConnection: Keep-Alive \ R & lt \ nSec -Fetch-Mode: no-cors \ r \ nUser-Agent: Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 76.0.3809.100 Safari / 537.36 \ r \ nAccept: image /webp,image/apng,image/*,*/*;q=0.8\r\nSec-Fetch-Site: same-origin \ r \ nReferer: http://127.0.0.1:8888/\r\nAccept- Encoding: the gzip, the deflate, br \ R & lt \ nAccept-Language: the CN-ZH, ZH; 0.9 = Q \ R & lt \ n-\ R & lt \ n- '
the server response data in response to a client, comprising a status line (protocol version space condition code space Status Code Description carriage return linefeed), response header (Key: value carriage return linefeed: value carriage return linefeed * 2), response data
HTTP/1.1 200 OK\r\nContent-Length: 643\r\nContent-Type: text/plain; charset=UTF-8\r\nDate: Thu, 15 Aug 2019 06:01:10 GMT
Uniform Resource Locator: Port path protocol address parameters
Request header generally browser information, cookie
Usually the response header encoding, time
3. Create a simple http server
import socket ser1 =socket.socket() ser1.bind(('127.0.0.1',8888)) ser1.listen(5) while 1: conn.addr = ser1.accept() data = conn.recv(1024) print(data) #请求request数据
conn.send (b'HTTP / 1.1 200 OK \ r \ nContent-Type: text / html; charset = UTF-8 \ r \ n \ r \ n ') # http protocol response data
conn.send('<h1>hello world</h1>'.encode('utf-8')) #数据
conn.close()
ser1.close()
4. dynamic pages and static pages
Dynamic page is to get the data back end interact with the display page, the page is to display static pages can not interaction
The server application program and
Three large web applications: 1. 2. Back responsible socket connection request differ depending dynamic page address path 3. Return
The server will finish part of the socket connector, but it is not concerned with the connection request processing business logic code
Application business logic to complete the relevant code django framework to complete this part of the return to what page, regardless of how the client is connected
Question: How do the rules of interaction between the server and the application program, wsgi Server Gateway Interface (production uwsgi, django default the wsgiref))
https://blog.csdn.net/qq_38038143/article/details/80291234 nginx+uwsgi+django
Other framework of 6.python
django completed business logic code + dynamic page return
flask completed business logic code
tornado completed the business logic code server program + + dynamic page Back
7. Design software architecture model
1) MVC framework
controller+model+view
Controller: business logic, access address, execute the function
Model: Avoid sql database model
View: Display of dynamic pages
2) MTV framework
According mvc django redesigned look model + templates + view
Model: orm object-relational database model
Template: Responsible presented to the user's page
View: responsible for business logic
Additional: url routing distributor
Compare the two:
mvc can be said to be generalized, the controller correspond mtv view functions and routing, view the corresponding template, the model has been
The 8.django request and reponse objects
REQUEST request is a view of the object in the function necessary parameters, object encapsulates all the http protocol information in this request
Method request.method # type of request, the dispatch method in cbv will match the list of known type, we obtain the reflection custom execution
request.GET.get ( 'keyword') # get parameters from a GET request, you can get the url? argument behind
request.GET.urlencode () # acquisition parameters on the GET request, and encodes
request.POST.get ( 'keyword') # acquires the content from the values in the form of a POST request, the content may be submitted in the form obtained according to this method
Request.path_info # parameter acquisition request path information, it does not contain an address port,? After
Request.body # request data request body, there are only post request with the plaintext
request.scheme # The request protocol http https
request.encoding # obtain encoding data submitted
request.COOKIES # get COOKIES
request.session # get session information
request.FILES # used to upload files into the required form and form enctype = "multipart / form-data"
Response response several ways
The HttpResponse ( 'string') directly back # html tags or strings
the render (request, 'template file name,' { 'Parameter 1': 1 'parameter 2': 2}) # returns template pages, and may also return a parameter request object to the template file name
redirect (redirect) # redirected to another url
JsonResponse (data, safe = False) # json type data into the return, if the data dictionary may be used without safe = False
9. File Upload simple example
####urls.py url(r'^file/', views.file.as_view(), name='file'), ####views.py class file(View): def post(self, request): upfile = request.FILES.get('upfile') with open('testfile.txt', mode='w', encoding='utf-8') as f1: for i in upfile.chunks(): f1.write(i.decode('utf-8')) return HttpResponse('上传完成') ####html模板 <form action="{% url 'file' %}" enctype="multipart/form-data" method="post"> <input type="file" name="upfile"> <button type="submit">上传</button> </form>