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

  • 相关阅读:
    BZOJ2243: [SDOI2011]染色
    BZOJ3747: [POI2015]Kinoman
    BZOJ1293: [SCOI2009]生日礼物
    BZOJ3626 [LNOI2014]LCA
    BZOJ3514 Codechef MARCH14 GERALD07加强版
    BZOJ3295 [CQOI2011]动态逆序对
    BZOJ2588 [SPOJ10628]Count on a tree
    BZOJ1036 [ZJOI2008]树的统计Count
    CODEVS1490 [CTSC2008]网络管理
    BZOJ1070 [SCOI2007]修车
  • 原文地址:https://www.cnblogs.com/JewinH/p/8352511.html
Copyright © 2011-2022 走看看