1. 程式人生 > >Python爬蟲最為核心的HTTP協議解析,及自定義協議的分析!

Python爬蟲最為核心的HTTP協議解析,及自定義協議的分析!

內容 proc 最新 ges font -o solid head nor

機器之間的協議就是機器通信的語法,只有按照這種語法發來的信息,機器之間才能相互理解內容,也可以理解為信息的一種格式。

HTTP/IP協議是互聯網最為重要的協議,沒有HTTP/IP協議,也就沒有互聯跟不會有網,對於爬蟲而言一切數據、請求都是圍繞HTTP協議展開。
技術分享圖片
但是在python實現的網絡爬蟲中都是使用封裝好了的請求庫如:requests、scrapy、urllib等,這些是對socket的封裝,而socket是除了機器語言外最底層的協議。

HTTP是公認的協議,但是並不是所有的終端通信都使用HTTP協議,也有處於保密需求而自定義協議,我們要通過對HTTP協議的分析理解來認來掌握自定義協議的分析思路。

技術分享圖片
在瀏覽器開發者模式下,任意截獲一個數據包點擊view parsed,顯示出來的就是原始的HTTP請求頭格式及協議請求頭格式。
技術分享圖片
最主要的頭兩行分析如下,第一行:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> GET / HTTP/1.1 分別是請求方式 請求路徑 協議及其版本

</pre>

/就表示首頁,最後的HTTP/1.1指示采用的HTTP協議版本是1.1

從第二行開始,每一行都類似於Xxx: abcdefg:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> Host: mzzd.xyz

</pre>

表示請求的域名是mzzd.xyz,如果一臺服務器有多個網站,服務器就需要通過Host來區分瀏覽器請求的是哪個網站。

再看HTTP響應及其格式:
技術分享圖片
HTTP響應分為Header和Body兩部分(Body是可選項),我們在Network中看到的Header最重要的幾行如下:

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> HTTP/1.1 200 OK 分別是協議版本 狀態碼 說明

</pre>

200表示一個成功的響應,後面的OK是說明。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> Content-Type: text/html

</pre>

Content-Type指示響應的內容,瀏覽器依靠Content-Type來判斷響應的內容類型,即使URL是http://www.mzzd.xyz/1.jpg,它也不一定就是圖片。
技術分享圖片
那我們用python實現HTTP客戶端:
技術分享圖片
這個客戶端的作用就是當你在瀏覽器訪問本地的8000端口,會向瀏覽器返回hello Word的字符。
技術分享圖片
說了這麽多應該明白HTTP協議其實只是一段文本,只是文本首行是協議頭,一空行之後是協議體,按照這種格式那麽瀏覽器就能解析。
技術分享圖片
而自定義的協議常見於APP中,因為在APP中定義彼此通信的過程、定義通信中相關字段的含義,即使被抓包後那也很難解析,這種情況很少,因為有現成的協議何必去自己弄一套協議,但是遇到之後應該明白如何下手。

甚至可以開發自己基於socket的爬蟲庫。
技術分享圖片
結語

如果你跟我一樣都喜歡python,想成為一名優秀的程序員,也在學習python的道路上奔跑,歡迎你加入python學習群:839383765 群內每天都會分享最新業內資料,分享python免費課程,共同交流學習,讓學習變(編)成(程)一種習慣!

Python爬蟲最為核心的HTTP協議解析,及自定義協議的分析!