zoukankan      html  css  js  c++  java
  • http chunked 理解

    https://imququ.com/post/transfer-encoding-header-in-http.html

    #! /usr/bin/python
    #coding:utf8
    
    import socket
    import signal
    import errno
    
    from time import sleep
    
    def sig_handler(signo, frame):
        print "get signo ", signo
        global runFlg
        runFlg = False
        global listeningFd
        listeningFd.shutdown(socket.SHUT_RD)
    
    def http_response_with_content_length(sock):
        """ 有长度没有 close """
        sock.send('HTTP/1.1 200 OK
    ')
        sock.send('Content-Length: 12
    ')
        sock.send('
    ')
        sock.send('Hello World!')
    
    def http_response_with_close(sock):
        """" 没有长度有 close """
        sock.send('HTTP/1.1 200 OK
    ')
        sock.send('
    ')
        sock.send('Hello World!')
        sock.close()
    
    def http_response_chunked(sock):
        """" 没有长度,没有 close """
        sock.send('HTTP/1.1 200 OK
    ')
        sock.send('Transfer-Encoding: chunked
    ')
        sock.send('
    ')
    
        # 第一个 chunk
        sock.send('a
    ')
        sock.send('1234567890
    ')
    
        # 第二个 chunk
        sock.send('5
    ')
        sock.send('12345
    ')
    
        # 第三个 chunk,也是最后一个
        sock.send('0
    ')
        sock.send('
    ')
    
    def http_response(sock):
        """" 没有长度,没有 close """
        sock.send('HTTP/1.1 200 OK
    ')
        sock.send('
    ')
        sock.send('Hello World!')
    
    signal.signal(signal.SIGINT, sig_handler)
    
    HOST = "127.0.0.1"
    PORT = 37001
    
    listeningFd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    listeningFd.bind((HOST, PORT))
    listeningFd.listen(2)
    
    print "HttpServer Started"
    
    runFlg = True
    while runFlg:
        try:
            connectedFd, addr = listeningFd.accept()
        except socket.error as e:
            if e.errno == errno.EINT:
                print "get a except EINTR"
            else:
                raise
            continue
            
        if runFlg == False:
            break
            
        print "connected by ", addr
        
        data = connectedFd.recv(1024)
        if not data:
            break
        print data
        #http_response_with_close(connectedFd)               # 流量器能正确处理
        #http_response(connectedFd)                          # 浏览器不能正确处理 pending
        #http_response_with_content_length(connectedFd)      # 浏览器能正确处理
        http_response_chunked(connectedFd)                  # 浏览器能正确处理
    else:
        print "runFlg ", runFlg
        
    print "Done"
  • 相关阅读:
    收藏CSS经典技巧
    理解这26句话将不再烦恼
    包转发率得计算和背板带宽的计算
    mysql 建表 AUTO_INCREMENT , 数据类型 VARCHAR
    Linux Wine with *.bat *.exe ( Photoshop and etc.. )
    [转载]expect spawn、linux expect 用法小记
    sqlmap.py Database injection and hak
    xls===>csv tables===via python ===> sqlite3.db
    sftp 服务器外网访问设置
    vsftp FTP服务器外网访问设置
  • 原文地址:https://www.cnblogs.com/wendellyi/p/7205176.html
Copyright © 2011-2022 走看看