首頁 > 軟體

Python socket如何解析HTTP請求內容

2022-02-12 16:00:23

socket解析HTTP請求內容

思路

1. 解析HTTP請求的頭部

HTTP請求頭部的結束符行為"rn",可以按行讀取HTTP請求頭的內容,如果讀到一行為"rn",說明HTTP請求頭結束。

2. 請求頭裡面含有Content-Length引數

如果HTTP請求裡面有Content-Length引數,說明HTTP請求的內容大小是確定的,請求直接讀取Content-Length的值,然後讀取相應位元組的的內容即可。

3. 請求頭裡面含有Transfer-Encoding: chunked 引數

如果HTTP請求裡面有Transfer-Encoding引數,說明HTTP請求的內容大小是不確定的,這種內容的結束符是"0rnrn",因此可以按行讀取HTTP請求的內容部分,如果連續讀到"0rn"和"rn"說明內容讀取完畢。

程式碼實現

程式碼中: 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頭結束符
            # 讀取http內容
            if req_line == "rn":
                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 == "0rn":
                            sub_line = self._file.readline()
                            sub_line = str(sub_line, "utf-8")
                            if sub_line == "rn":
                                self._content = content
                                return None
                        else:
                            content += line
                            continue
                    self._content = False
 
            # 標頭檔案沒有結束
            # 並且沒有找到關於內容大小的欄位
            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.1rnHost:{}rnConnection:closernrn".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("rnrn")[1]
    print(html_data)
    client.close()
    pass
if __name__ == '__main__':
    get_url("http://www.baidu.com")

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com