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("\n".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


阅读更多
文章标签: http web server
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭