Python?socket如何解析HTTP請(qǐng)求內(nèi)容
socket解析HTTP請(qǐng)求內(nèi)容
思路
1. 解析HTTP請(qǐng)求的頭部
HTTP請(qǐng)求頭部的結(jié)束符行為"\r\n",可以按行讀取HTTP請(qǐng)求頭的內(nèi)容,如果讀到一行為"\r\n",說(shuō)明HTTP請(qǐng)求頭結(jié)束。
2. 請(qǐng)求頭里面含有Content-Length參數(shù)
如果HTTP請(qǐng)求里面有Content-Length參數(shù),說(shuō)明HTTP請(qǐng)求的內(nèi)容大小是確定的,請(qǐng)求直接讀取Content-Length的值,然后讀取相應(yīng)字節(jié)的的內(nèi)容即可。
3. 請(qǐng)求頭里面含有Transfer-Encoding: chunked 參數(shù)
如果HTTP請(qǐng)求里面有Transfer-Encoding參數(shù),說(shuō)明HTTP請(qǐng)求的內(nèi)容大小是不確定的,這種內(nèi)容的結(jié)束符是"0\r\n\r\n",因此可以按行讀取HTTP請(qǐng)求的內(nèi)容部分,如果連續(xù)讀到"0\r\n"和"\r\n"說(shuō)明內(nèi)容讀取完畢。
代碼實(shí)現(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 # 頭文件沒(méi)有結(jié)束 # 并且沒(méi)有找到關(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請(qǐng)求
# 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")
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python直接訪問(wèn)私有屬性的簡(jiǎn)單方法
下面小編就為大家?guī)?lái)一篇python直接訪問(wèn)私有屬性的簡(jiǎn)單方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07利用python實(shí)現(xiàn)對(duì)web服務(wù)器的目錄探測(cè)的方法
這篇文章主要介紹了利用python實(shí)現(xiàn)對(duì)web服務(wù)器的目錄探測(cè)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02python中的elasticsearch_dsl查詢語(yǔ)句轉(zhuǎn)換成es查詢語(yǔ)句詳解
這篇文章主要介紹了python中的elasticsearch_dsl查詢語(yǔ)句轉(zhuǎn)換成es查詢語(yǔ)句詳解,ElasticSearch在實(shí)際生產(chǎn)里通常和LogStash,Kibana,F(xiàn)ileBeat一起構(gòu)成Elastic?Stack來(lái)使用,它是這些組件里面最核心的一個(gè),需要的朋友可以參考下2023-07-07