這篇文章主要為大家介紹了關於利用python的socket發送http(s)請求的相關資料,文中透過範例程式碼介紹的非常詳細,對大家學習或使用python具有一定的參考學習價值,需要的朋友們下面來一起看看吧
前言
#這是個在寫電腦網路課設的時候碰到的問題,卡了我一天,所以總結一下。
其實在之前就有用requests寫過python爬蟲,但是電腦網路要求更底層的實現,剛好我看到了[這篇文章]1結果發現他就是用socket來實現的請求,所以就學習了。
本來也覺得應該不難,畢竟就是建立tcp連線。
原始網站的範例如下:
def fetch(url): sock = socket.socket() # 建立socket sock.connect(('xkcd.com', 80)) # 远程连接 request = 'GET {} HTTP/1.0\r\nHost: xkcd.com\r\n\r\n'.format(url) # 构建请求 sock.send(request.encode('ascii')) # 向socket发送数据 response = b'' chunk = sock.recv(4096) # 从socket接收数据 while chunk: response += chunk chunk = sock.recv(4096) # Page is now downloaded. links = parse_links(response) q.add(links)
我選擇爬取的網站是鍊家,當然也看了很多其他例子,還用fiddler抓包,把headers整個放了上去,首先是參考了這個文章:https://segmentfault.com/a/1190000005126160 ,該文章介紹到:
Python通過socket發送http請求
我們以存取百度主頁為案例,使用socket發送http請求。
import socket s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect(('www.baidu.com',80)) s.send('''GET https://www.baidu.com/ HTTP/1.1 Host: www.baidu.com Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 Accept-Language: zh-CN,zh;q=0.8 ''') buf=s.recv(1024) while len(buf): print buf buf = s.recv(1024)
基於socket進行http程式設計在於請求參數的可控性更好,但是難度相應就更大了。以上發送的資料是直接從fiddler抓包器裡面複製的。
基於以上,寫出如下程式碼:
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect(('www.baidu.com',80)) s.send('''GET / HTTP/1.1 Host: zh.lianjia.com Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Referer: https://www.baidu.com/link?url=4J5Kx--GLdLFESJhkfRePU8Ac_0agnTcOtB-b3kfnX8VNdZ_6TPqOyJGKVXkTczg&ck=6140.3.83.296.315.287.208.155&shh=www.baidu.com&sht=94886267_hao_pg&wd=&eqid=af98b98700060b77000000065aef0524 Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9,en-CA;q=0.8,en;q=0.7 Cookie: lianjia_uuid=ce61c41c-25b0-46d6-a0a0-d57a75ee8706; UM_distinctid=1631f588055f9-0286722badd3ec-b34356b-1fa400-1631f58805657f; _ga=GA1.2.43397143.1525239286; _smt_uid=5ae94e02.558be516; _jzqx=1.1525248800.1525335927.1.jzqsr=zh%2Elianjia%2Ecom|jzqct=/ershoufang/xiangzhouqu/.-; _jzqc=1; _jzqckmp=1; _gid=GA1.2.1028411676.1525594529; select_city=440400; all-lj=c60bf575348a3bc08fb27ee73be8c666; _qzjc=1; CNZZDATA1254525948=963210960-1525238218-https%253A%252F%252Fwww.lianjia.com%252F%7C1525608956; CNZZDATA1255633284=1054798284-1525238580-https%253A%252F%252Fwww.lianjia.com%252F%7C1525608969; lianjia_ssid=c046ddb3-3e66-4809-998a-52ade335fdfc; _qzja=1.1070225156.1525239298260.1525603274282.1525613866775.1525609113492.1525613866775.0.0.0.92.9; _qzjto=29.3.0; _jzqa=1.3750161754444366000.1525239284.1525603274.1525613867.9; _jzqy=1.1525239284.1525613867.3.jzqsr=baidu.jzqsr=baidu; Hm_lvt_9152f8221cb6243a53c83b956842be8a=1525607433,1525607626,1525609113,1525613867; Hm_lpvt_9152f8221cb6243a53c83b956842be8a=1525613867; _qzjb=1.1525613866775.1.0.0.0; _jzqb=1.1.10.1525613867.1; CNZZDATA1255604082=964175865-1525237915-https%253A%252F%252Fwww.lianjia.com%252F%7C1525612833 ''')
#結果總是報400(Bad Request)
,這個地方卡了很久,最後解決方法是一條一條發送,每一條後面加\r\n。
sock = socket.socket() sock.connect(('zh.lianjia.com', 80)) sock.send('GET /ershoufang/ HTTP/1.1\r\n'.encode()) sock.send('Host: zh.lianjia.com\r\n'.encode()) sock.send('Connection: keep-alive\r\n'.encode()) sock.send('Cache-Control: no-cache\r\n'.encode()) sock.send('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\r\n'.encode()) sock.send('Upgrade-Insecure-Requests: 1\r\n'.encode()) sock.send('User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36\r\n'.encode()) sock.send('Accept-Encoding: gzip, deflate, br\r\n'.encode()) sock.send('Cookie: lianjia_uuid=ce61c41c-25b0-46d6-a0a0-d57a75ee8706; UM_distinctid=1631f588055f9-0286722badd3ec-b34356b-1fa400-1631f58805657f; _ga=GA1.2.43397143.1525239286; _smt_uid=5ae94e02.558be516; _jzqx=1.1525248800.1525335927.1.jzqsr=zh%2Elianjia%2Ecom|jzqct=/ershoufang/xiangzhouqu/.-; _jzqc=1; _jzqy=1.1525239284.1525594526.2.jzqsr=baidu.jzqsr=baidu|jzqct=%E9%93%BE%E5%AE%B6; _jzqckmp=1; _gid=GA1.2.1028411676.1525594529; Hm_lvt_9152f8221cb6243a53c83b956842be8a=1525594526,1525594536,1525594804,1525595210; select_city=440400; all-lj=c60bf575348a3bc08fb27ee73be8c666; _qzjc=1; lianjia_ssid=99306d63-8ee5-a53c-a740-2d3021f3db2f; CNZZDATA1255604082=964175865-1525237915-https%253A%252F%252Fwww.lianjia.com%252F%7C1525602095; _jzqa=1.3750161754444366000.1525239284.1525594526.1525603274.8; CNZZDATA1254525948=963210960-1525238218-https%253A%252F%252Fwww.lianjia.com%252F%7C1525603556; CNZZDATA1255633284=1054798284-1525238580-https%253A%252F%252Fwww.lianjia.com%252F%7C1525603557; Hm_lpvt_9152f8221cb6243a53c83b956842be8a=1525606057; _jzqb=1.9.10.1525603274.1; _qzja=1.1070225156.1525239298260.1525597069547.1525603274282.1525605398368.1525606071025.0.0.0.86.8; _qzjb=1.1525603274282.9.0.0.0; _qzjto=23.2.0\r\n\r\n'.encode())
結果總是重定向,狀態碼301!找了好久都不知道什麼原因,而且直接在瀏覽器網址列輸入網址,用fiddler抓包也沒有抓到狀態為301的包。最後使用fiddler的composer輸入http://zh.lianjia.com/ershoufang 就抓到了301和200,其中200的地址就是https://zh.lianjia.com/ershoufang,如下圖。
這下知道原因了,是http和https的差別。 (其實301狀態碼的時候應答部分的Location可以觀察到,但是一個s太不顯眼了所以我沒有註意到,導致卡了很久)
接下來只要知道如何發送https請求就好了。下面是程式碼,主要是更改建立socket和連接的部分。注意連接埠號碼為443。參考文章在這裡
sock = ssl.wrap_socket(socket.socket()) sock.connect(('zh.lianjia.com', 443))
感覺自己很多地方了解的不夠深入,暫時學校也沒有講到應用層。到時候再研究研究,如有錯漏歡迎指出。
相關推薦:
利用python執行shell腳本並動態傳參及subprocess基本使用
#
以上是利用python的socket發送http(s)請求方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!