Python使用Socket(Https)Post登錄百度的實(shí)現(xiàn)代碼
更新時(shí)間:2012年05月18日 17:47:11 作者:
以前都是用一些高級(jí)模塊,封裝的比較好,今天嘗試使用socket模塊登錄百度,弄了半天才弄好,主要由于百度在登陸頁(yè)使用了https,我們需要對(duì)socket進(jìn)行一定處理
登錄百度,首先當(dāng)然是先抓百度的登錄包 ,由于是網(wǎng)頁(yè)登錄,最方便的自然是httpwatch了,我使用的測(cè)試賬號(hào)是itiandatest1,密碼是itianda,抓包結(jié)果:
復(fù)制代碼 代碼如下:
POST /?login HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: https://passport.baidu.com/?login&tpl=mn
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Alexa Toolbar; BOIE9;ZHCN)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: passport.baidu.com
Content-Length: 243
Connection: Keep-Alive
Cache-Control: no-cache
登錄包抓到了,下面開(kāi)始寫代碼:
復(fù)制代碼 代碼如下:
import socket
import ssl
sock = ssl.wrap_socket(socket.socket())
ssl是專門用來(lái)處理https的模塊,我們使用該模塊的wrap_socket函數(shù)生成一個(gè)SSLSocket對(duì)象。
然后建立連接:
復(fù)制代碼 代碼如下:
sock.connect(('passport.baidu.com', 443))
這里需要注意的是https使用443端口,不是80。
之后發(fā)送數(shù)據(jù):
復(fù)制代碼 代碼如下:
data = '''\
POST /?login HTTP/1.1
Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: https://passport.baidu.com/?login&tpl=mn
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Alexa Toolbar; BOIE9;ZHCN)
Content-Type: application/x-www-form-urlencoded
Host: passport.baidu.com
Content-Length: 243
Connection: Keep-Alive
Cache-Control: no-cache
tpl_ok=&next_target=&tpl=mn&skip_ok=&aid=&need_pay=&need_coin=&pay_method=&u=http%3A%2F%2Fwww.baidu.com%2F&return_method=get&more_param=&return_type=&psp_tt=0&password=itianda&safeflg=0&isphone=tpl&username=itiandatest1&verifycode=&mem_pass=on\
'''
sock.sendall(data)
需要注意的是sendall之后不能調(diào)用shutdown方法。
其余部分就和普通的socket處理方式?jīng)]什么差別了 :
復(fù)制代碼 代碼如下:
recv_data = sock.recv(8192)
sock.close()
print recv_data
由于我們只需要cookie信息,所以只接收少量數(shù)據(jù)就可以了。
登錄成功的標(biāo)志是服務(wù)器返回含有BDUSS的set-cookie:
復(fù)制代碼 代碼如下:
HTTP/1.1 200 OK
Set-Cookie: BAIDUID=DB464E1EBA6571FB82D70460D6AAB666:FG=1; max-age=946080000; expires=Wed, 11-Dec-41 17:18:17 GMT; domain=.baidu.com; path=/; version=1
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Date: Mon, 19 Dec 2011 17:18:17 GMT
Server: Apache
P3P: CP=" OTI DSP COR IVA OUR IND COM "
P3P: CP=" OTI DSP COR IVA OUR IND COM "
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Set-Cookie: BAIDUID=26FD0CB5389BF4699C447982D8080239:FG=1; expires=Wed, 11-Dec-41 17:18:17 GMT; max-age=946080000; path=/; domain=.baidu.com; version=1
Set-Cookie: BAIDUID=26FD0CB5389BF4698191E4134CACEA29:FG=1; expires=Wed, 11-Dec-41 17:18:17 GMT; max-age=946080000; path=/; domain=.baidu.com; version=1
Set-Cookie: BDUSS=dTajkzWTFWR3hXT3Jsc09LdkNsZ011YlZka340VWtqNkZzbW0tUTdOUFp-aFpQQVFBQUFBJCQAAAAAAAAAAAouTSCLkioVaXRpYW5kYXRlc3QxAAAAAAAAAAAAAAAAAAAAAAAAAADgmoV5AAAAAOCahXkAAAAAuWZCAAAAAAAxMC42NS40NNlx707Zce9OWT; expires=Tue, 01 Jan 2030 00:00:00 GMT; path=/; domain=.baidu.com
Set-Cookie: PTOKEN=16ba4a120f070f3cc759a817981c2516; expires=Tue, 01 Jan 2030 00:00:00 GMT; path=/; domain=passport.baidu.com; HttpOnly
Set-Cookie: STOKEN=fda94395cd4ae4661cefd3a4017a8454; expires=Tue, 01 Jan 2030 00:00:00 GMT; path=/; domain=passport.baidu.com
Set-Cookie: USERID=626167789a799e630e60fb27466fa80e; expires=Tue, 01 Jan 2030 00:00:00 GMT; path=/; domain=.baidu.com
Content-Type: text/html;charset=gbk
Cache-Control: no-cache
Pragma: no-cache
Content-Encoding: none
Content-Length: 850
Connection: close
OK,登陸成功。
本文來(lái)自: itianda's blog
您可能感興趣的文章:
- Python 網(wǎng)絡(luò)編程起步(Socket發(fā)送消息)
- python socket網(wǎng)絡(luò)編程步驟詳解(socket套接字使用)
- python實(shí)現(xiàn)socket端口重定向示例
- python socket 超時(shí)設(shè)置 errno 10054
- Python通過(guò)websocket與js客戶端通信示例分析
- python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(三):socket網(wǎng)絡(luò)服務(wù)器
- python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(二):socket建立網(wǎng)絡(luò)客戶端
- python網(wǎng)絡(luò)編程之TCP通信實(shí)例和socketserver框架使用例子
- 為python設(shè)置socket代理的方法
- Pythony運(yùn)維入門之Socket網(wǎng)絡(luò)編程詳解
相關(guān)文章
Python實(shí)現(xiàn)搜索算法的實(shí)例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)搜索算法,文中通過(guò)實(shí)例代碼給大家分享高級(jí)搜索算法的想法,分為線性搜索和插值搜索,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01PyTorch使用GPU加速計(jì)算的實(shí)現(xiàn)
PyTorch利用NVIDIA CUDA庫(kù)提供的底層接口來(lái)實(shí)現(xiàn)GPU加速計(jì)算,本文就來(lái)介紹一下PyTorch使用GPU加速計(jì)算的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02Python可變參數(shù)函數(shù)用法實(shí)例
這篇文章主要介紹了Python可變參數(shù)函數(shù)用法,實(shí)例分析了Python可變參數(shù)函數(shù)的定義與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07