網站首頁 編程語言 正文
socket解析HTTP請求內容
思路
1. 解析HTTP請求的頭部
HTTP請求頭部的結束符行為"\r\n",可以按行讀取HTTP請求頭的內容,如果讀到一行為"\r\n",說明HTTP請求頭結束。
2. 請求頭里面含有Content-Length參數
如果HTTP請求里面有Content-Length參數,說明HTTP請求的內容大小是確定的,請求直接讀取Content-Length的值,然后讀取相應字節的的內容即可。
3. 請求頭里面含有Transfer-Encoding: chunked 參數
如果HTTP請求里面有Transfer-Encoding參數,說明HTTP請求的內容大小是不確定的,這種內容的結束符是"0\r\n\r\n",因此可以按行讀取HTTP請求的內容部分,如果連續讀到"0\r\n"和"\r\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頭結束符 # 讀取http內容 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 # 頭文件沒有結束 # 并且沒有找到關于內容大小的字段 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")
原文鏈接:https://blog.csdn.net/m0_37954775/article/details/100114334
相關推薦
- 2023-02-05 Flutter實現固定header底部滑動頁效果示例_Android
- 2022-08-28 Go讀寫鎖操作方法示例詳解_Golang
- 2022-08-06 為Visual?Studio手工安裝微軟ReportViewer控件_自學過程
- 2023-12-12 SSM整合 spring-mybaits配置文件——設置數據庫字段名駝峰命名規則
- 2024-01-14 springboot-mybatis/JPA流式查詢
- 2023-03-15 k8s中pod使用詳解(云原生kubernetes)_云其它
- 2022-03-18 webpack的懶加載和預加載詳解(webpack按需加載)
- 2022-04-12 pandas將DataFrame的幾列數據合并成為一列_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支