溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python?socket怎么解析HTTP請求內(nèi)容

發(fā)布時間:2022-02-14 09:42:34 來源:億速云 閱讀:396 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Python socket怎么解析HTTP請求內(nèi)容”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Python socket怎么解析HTTP請求內(nèi)容”吧!

    socket解析HTTP請求內(nèi)容

    思路

    1. 解析HTTP請求的頭部

    HTTP請求頭部的結(jié)束符行為"\r\n",可以按行讀取HTTP請求頭的內(nèi)容,如果讀到一行為"\r\n",說明HTTP請求頭結(jié)束。

    2. 請求頭里面含有Content-Length參數(shù)

    如果HTTP請求里面有Content-Length參數(shù),說明HTTP請求的內(nèi)容大小是確定的,請求直接讀取Content-Length的值,然后讀取相應(yīng)字節(jié)的的內(nèi)容即可。

    3. 請求頭里面含有Transfer-Encoding: chunked 參數(shù)

    如果HTTP請求里面有Transfer-Encoding參數(shù),說明HTTP請求的內(nèi)容大小是不確定的,這種內(nèi)容的結(jié)束符是"0\r\n\r\n",因此可以按行讀取HTTP請求的內(nèi)容部分,如果連續(xù)讀到"0\r\n"和"\r\n"說明內(nèi)容讀取完畢。

    代碼實現(xiàn)

    代碼中: self._file 代表的是socket.makefile() 

     def get_http_content(self):
            content_length = 0
            transfer_encoding = False
            while True:
                req_line = self._file.readline()
                req_line = str(req_line, "utf-8")
     
                # 遇到http頭結(jié)束符
                # 讀取http內(nèi)容
                if req_line == "\r\n":
                    if content_length != 0:
                        content = self._file.read(content_length)
                        content = str(content, "utf-8")
                        self._content = content
                        return None
     
                    if transfer_encoding:
                        content = ""
                        self._file.readline()
                        while True:
                            line = self._file.readline()
                            line = str(line, "utf-8")
                            if line == "0\r\n":
                                sub_line = self._file.readline()
                                sub_line = str(sub_line, "utf-8")
                                if sub_line == "\r\n":
                                    self._content = content
                                    return None
                            else:
                                content += line
                                continue
                        self._content = False
     
                # 頭文件沒有結(jié)束
                # 并且沒有找到關(guān)于內(nèi)容大小的字段
                else:
                    if content_length == 0 and transfer_encoding is False:
                        words = req_line.split()
                        if words[0] == "Content-Length:":
                            content_length = int(words[1])
                        if words[0] == "Transfer-Encoding:":
                            transfer_encoding = True
     
                self._content = False

    socket 模擬http請求

    # coding: utf-8
    import socket
    from urllib.parse import urlparse
    def get_url(url):
        url = urlparse(url)
        host = url.netloc
        path = url.path
        if path == "":
            path = "/"
        # 建立 socket 連接
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect((host, 80))
        client.send("GET {} HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n".format(path, host).encode("utf-8"))
        data = b""
        while True:
            d = client.recv(1024)
            if d:
                data += d
            else:
                break
        data = data.decode("utf-8")
        html_data = data.split("\r\n\r\n")[1]
        print(html_data)
        client.close()
        pass
    if __name__ == '__main__':
        get_url("http://www.baidu.com")

    到此,相信大家對“Python socket怎么解析HTTP請求內(nèi)容”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

    向AI問一下細節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI