Web-2

웹 - 1 작업 상세 설명
    url = 'http://movie.douban.com/top250'
    print('url[:0]:{}'.format(url[:0]))
    print('url[:1]:{}'.format(url[:1]))
    print('url[:2]:{}'.format(url[:2]))
    print('url[:3]:{}'.format(url[:3]))
    print('url[:5]:{}'.format(url[:5]))
    print('url[:7]:{}'.format(url[:7]))
    print('url[:100]:{}'.format(url[:100]))
    u = url.split('://')[1]
        x = url.split('://')[0]
        print('u:{}'.format(u))
        print('x:{}'.format(x))

url[:0]: url[:1]:h url[:2]:ht url[:3]:htt url[:5]:http: url[:7]:http:// url[:100]:http://movie.douban.com/top250 u:movie.douban.com/top250 x:http
url. split (': / /') [1] 은: / / 를 경계 로 두 문자열 배열 로 나 누고 0 은 첫 번 째 이 고 1 은 두 번 째 부분 입 니 다.
def parsed_url(url):
    # https://g.cn:1234/hello
    protocol = 'http'

    if url[:7] == 'http://':
        u = url.split('://')[1]
        x = url.split('://')[0]

    elif url[:8] == 'https://':
        protocol = 'https'
        u = url.split('://')[1]
    else:
        # '://'           /       
        u = url
    #g.cn:1234/hello
    i = u.find('/')
    print('i:{}'.format(i))
    if i == -1:
        host = u
        path = '/'
        print('host:{}path:{}'.format(u[:i], path))

    else:
        host = u[:i]
        print('u[:i]:{}'.format(u[:i]))
        path = u[i:]
        print('u[i:]:{}'.format(u[i:]))

    #     
    port_dict = {
            'http': 80,
            'https': 443,
    }
        #     
    port = port_dict[protocol]
    if ':' in host:
        h = host.split(':')
        host = h[0]

        port = int(h[1])
        print('host:{} port:{}'.format(host,port))
    return protocol, host, port, path

def main():
    url = 'g.cn:3000/search'
    parsed_url(url)

출력
i:9 u[:i]:g.cn:3000 u[i:]:/search host:g.cn port:3000
i 는 9, u [: i] 는 g. cn: 3000 과 같 습 니 다. 이것 은 9 글자 입 니 다. 1 부터 9 까지 모두 9 개 입 니 다. u [i:] 는 / search 와 같 습 니 다. 9 글자 부터 9 를 포함 하지 않 습 니 다.
테스트 함수
#    test           
def test_parsed_url():
    """
    parsed_url        ,                      
    """
    http = 'http'
    https = 'https'
    host = 'g.cn'
    path = '/'
    test_items = [
        ('http://g.cn', (http, host, 80, path)),
        ('http://g.cn/', (http, host, 80, path)),
        ('http://g.cn:90', (http, host, 90, path)),
        ('http://g.cn:90/', (http, host, 90, path)),
        #
        ('https://g.cn', (https, host, 443, path)),
        ('https://g.cn:233/', (https, host, 233, path)),
    ]
    for t in test_items:
        url, expected = t
        u = parsed_url(url)
        # assert      ,       
        #       ,     ,      
        #        ,       
        e = "parsed_url ERROR, ({}) ({}) ({})".format(url, u, expected)
        assert u == expected, e

get 함수
  • header, body = r.split('\r
    \r
    ', 1) 1 , , \r
    \r
    header body .
def parsed_response(r):

    header, body = r.split('\r
\r
', 1) h = header.split('\r
') status_code = h[0].split()[1] status_code = int(status_code) headers = {} print('h----:{}'.format(h)) print('h[1:]----:{}'.format(h[1:])) for line in h[1:]: print('line----:{}'.format(line)) k, v = line.split(': ') headers[k] = v return status_code, headers, body

h----:['HTTP/1.1 301 Moved Permanently', 'Date: Fri, 10 Nov 2017 04:49:05 GMT', 'Content-Type: text/html', 'Transfer-Encoding: chunked', 'Connection: close', 'Location: https://movie.douban.com/top250', 'Server: dae', 'X-Content-Type-Options: nosniff']
h[1:]----:['Date: Fri, 10 Nov 2017 04:49:05 GMT', 'Content-Type: text/html', 'Transfer-Encoding: chunked', 'Connection: close', 'Location: https://movie.douban.com/top250', 'Server: dae', 'X-Content-Type-Options: nosniff']
line----:Date: Fri, 10 Nov 2017 04:49:05 GMT
line----:Content-Type: text/html
line----:Transfer-Encoding: chunked
line----:Connection: close
line----:Location: https://movie.douban.com/top250
line----:Server: dae
line----:X-Content-Type-Options: nosniff

  • list . list, [1:] .

Web-2-2

server.py

  • (PEP8)
    , .
  • if name = 'main':
    , , main .
  • dict , map.set key .
    . .
config = dict(
        host='',
        port=3000,
    )

confing = {
        'host':'',
        'port': 3000
    }

Web-2-3

* GET&POST, request .
get path
post
.

GET
log ip and request, ('127.0.0.1', 63579)
GET /?author=tanyue&message=dahuaidan HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://localhost:3000/msg
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,ja;q=0.2
Cookie: SID=s%3Aj5EvLdf8fJbNZiOuNigTYhGDKgJkaOTT.geyhCc9aOaLLI%2B4bH0ivUxU8f5aSoWXV7sWWDzQDs1o; optimizelyEndUserId=oeu1509719645050r0.6015629543541059; _ga=GA1.1.1695961780.1490176344; Hm_lvt_dec99185042a5ffec601faced654a817=1509950985,1510121820,1510122239,1510138026; Hm_lpvt_dec99185042a5ffec601faced654a817=1510292868

POST
log ip and request, ('127.0.0.1', 63613)
POST /lqyj HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 31
Cache-Control: max-age=0
Origin: http://localhost:3000
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://localhost:3000/msg
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,ja;q=0.2
Cookie: SID=s%3Aj5EvLdf8fJbNZiOuNigTYhGDKgJkaOTT.geyhCc9aOaLLI%2B4bH0ivUxU8f5aSoWXV7sWWDzQDs1o; optimizelyEndUserId=oeu1509719645050r0.6015629543541059; _ga=GA1.1.1695961780.1490176344; Hm_lvt_dec99185042a5ffec601faced654a817=1509950985,1510121820,1510122239,1510138026; Hm_lpvt_dec99185042a5ffec601faced654a817=1510292938

author=tanyue&message=dahuaidan

authhor
post action .method post&get

좋은 웹페이지 즐겨찾기