The path to learning in Python -2018/6/14

Source: Internet
Author: User

The path to learning in Python -2018/6/14 1. Browser and server

The process by which the browser sends a request to the server is the process of requesting, and the server responds to the browser as a response process.

2. A simple Web application
ImportSocketsock=Socket.socket () Sock.bind ("100.113.14.43",8080)) Sock.listen (5) with Open("Index.html","R") asF:# First write the style to the HTML file, and then read the Send to the browserContent=F.read () while True:Print("Server starting ...") Conn, addr=Sock.accept () data=CONN.RECV (1024x768)Print("Data:", data) Conn.send (("http/1.1 OK\r\n\r\n%s" %Content). Encode ("UTF8"))# HTTP protocol formatConn.close () Sock.close ()

Index.html

<! DOCTYPE  html>    lang=  Span class= "st" > "en" >  <HEAD>    charset=   >  span class= "kw" ><TITLE>  title</TITLE>   <BODY>  <H1>  Hello world</ H1>    src=   >  </BODY>  </HTML>  

Next, use your browser to access 100.113.14.43:8080

3. Request Agreement

Request format

Get vs. Post differences

    • Get is used primarily to obtain database data and use post when updating database data
    • Get no request body, the submitted data is placed after the URL, through the. Delimited, between the parameters through & connection, for example, 100.113.14.43:8080/gyq?name=gyq&age=22;post submitted data stored in the request body
    • Due to the limited URL length, there is a limit to the data size of the get commit, and there is no limit to post commit data size

      Response agreement?

Response format

Response Status Code

type cause
1XX Information (information) The request is being processed
2XX Success (Success) Request Processing completed
3XX Redirection (redirected) Additional action required to complete the request
4XX Client error (Clients errors) The server cannot process the request
5XX Server error (server errors) Server Processing Request Error
 fromWsgiref.simple_serverImportMake_serverdefAppliaction (environ, start_response):# Environ: Parsing data by HTTP protocol    # Strat_response: Assemble data according to HTTP protocolPath=Environ.get ("Path_info") Start_response ("OK", []) with Open("Index.html","RB") asF:data1=F.read () with Open("Login.html","RB") asF:data2=F.read ()ifPath== "/index":return[Data1]elifPath== "/login":return[Data2]Else:return[b"]httpd=Make_server ("",8090, appliaction)Print("Server starting..")# Start listeningHttpd.serve_forever ()

The path to learning in Python -2018/6/14

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.