zoukankan      html  css  js  c++  java
  • HTTP&Web Servers——Udacity

    HTTP response construction:

    1.status code 

    2.header

    3.response 

    --------------------------------------------

    url construction:

    scheme://hostname/path

    such as : http://example.net/google.com/ponies

    http->scheme;example.net->hostname ; /google.com/ponies ->path

    hostname ->DNS -> IP

    port  is  a part of hostname 

    ----------------------------------------------

    HTTP methods 

    GET , POST 

    PUT ,DELECT ,PATCH

    ----------------------------------------------

    python 

    lib : http.server 

    function:

    do_http-methods()->  client send you a http method (for example : GET) , the server use do_GET function to deal with the get request.

    HTTPServer(server_address  ,  BaseHTTPRequestHandler) ->  create a server.

    HTTPServer . serve_forever() -> run a HTTPServer Object   forever.

    .read()   ;   .write()  ; decode()  ;  encode()  ; str.format () ; send_header() ; send_response() ; parse_qs()

    #!/usr/bin/env python3
    #
    # An HTTP server that's a message board.
    
    from http.server import HTTPServer, BaseHTTPRequestHandler
    from urllib.parse import parse_qs
    
    memory = []
    
    form = '''<!DOCTYPE html>
      <title>Message Board</title>
      <form method="POST">
        <textarea name="message"></textarea>
        <br>
        <button type="submit">Post it!</button>
      </form>
      <pre>
    {}
      </pre>
    '''
    
    
    class MessageHandler(BaseHTTPRequestHandler):
        def do_POST(self):
            # How long was the message?
            length = int(self.headers.get('Content-length', 0))
    
            # Read and parse the message
            data = self.rfile.read(length).decode()
            message = parse_qs(data)["message"][0]
    
            # Escape HTML tags in the message so users can't break world+dog.
            message = message.replace("<", "&lt;")
    
            # Store it in memory.
            memory.append(message)
    
            # Send a 303 back to the root page
            self.send_response(303)  # redirect via GET
            self.send_header('Location', '/')
            self.end_headers()
    
        def do_GET(self):
            # First, send a 200 OK response.
            self.send_response(200)
    
            # Then send headers.
            self.send_header('Content-type', 'text/html; charset=utf-8')
            self.end_headers()
    
            # Send the form with the messages in it.
            mesg = form.format("
    ".join(memory))
            self.wfile.write(mesg.encode())
    
    if __name__ == '__main__':
        server_address = ('', 8000)
        httpd = HTTPServer(server_address, MessageHandler)
        httpd.serve_forever()

    ---------------------------------

    JSON  - >  dictionary 

    ----------------------------------

    cache 

    --------------------------

    cookies

    -----------------------------------------------------------------

    web  security  - >  https ->  http/1.1  - > http/2 

    -------------------------------------------------

    Other   :  TCP   TLS   SSL  

    cloud: 链接:https://pan.baidu.com/s/1snqiu4l 密码:dtnn

  • 相关阅读:
    poj 2584 T-Shirt Gumbo (二分匹配)
    hdu 1757 A Simple Math Problem (乘法矩阵)
    矩阵之矩阵乘法(转载)
    poj 2239 Selecting Courses (二分匹配)
    hdu 3661 Assignments (贪心)
    hdu 1348 Wall (凸包)
    poj 2060 Taxi Cab Scheme (二分匹配)
    hdu 2202 最大三角形 (凸包)
    hdu 1577 WisKey的眼神 (数学几何)
    poj 1719 Shooting Contest (二分匹配)
  • 原文地址:https://www.cnblogs.com/JewinH/p/8352511.html
Copyright © 2011-2022 走看看