下文主要講述如何利用python自帶的庫模擬http請求,爲以html
#!coding:utf-8 相信這句你們都懂的,不解釋python
#導入須要的python模塊httplib,用來模擬提交http請求,詳細的用法可見python幫助手冊nginx
import httplib服務器
#導入須要的python模塊urllib,用來對數據進行編碼
import urllib
#定義請求頭cookie
reqheaders={'Content-type':'application/x-www-form-urlencoded',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Host':'www.renren.com',
'Origin':'http://zhichang.renren.com',
'Referer':'http://zhichang.renren.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1',}app
#定義post的參數dom
reqdata={'email':'xxxx@xxx.com',
'password':'xxxx',
'autoLogin':'on',
'origURL':'http://zhichang.renren.com/?login_state=rr',
'domain':'renren.com'
}post
#對請求參數進行編碼測試
data=urllib.urlencode(reqdata)編碼
#利用httplib庫模擬接口請求
#先鏈接到人人
conn=httplib.HTTPConnection('renren.com')
#提交登陸的post請求
conn.request('POST', '/PLogin.do', data, reqheaders)
#獲取服務器的返回
res=conn.getresponse()
#打印服務器返回的狀態
print(res.status)
#以dictionary形式答應服務器返回的 response header
print(res.msg)
#打印服務器返回請求頭中設置的cookie
print(res.getheader('Set-Cookie'))
#如下爲運行程序後的結果
登陸成功後重定向了
Server: nginx/1.2.0
Date: Sat, 15 Feb 2014 04:47:09 GMT
Content-Length: 80
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: http://zhichang.renren.com/?login_state=rr 重定向的URL
Set-Cookie: anonymid=hroelq3l-czxmdy; domain=.renren.com; path=/; expires=Thu, 14-Feb-2019 04:47:09 GMT
Set-Cookie: _de=97FB170A42B4342D1C47A157AD77AAFC1383380866D39FF5; domain=.renren.com; path=/; expires=Tue, 10-Feb-2015 04:47:09 GMT
Set-Cookie: p=31991a0a194c34e606ef1263317b06372; domain=renren.com; path=/; expires=Mon, 17-Mar-2014 04:47:09 GMT
Set-Cookie: ap=229996362; domain=renren.com; path=/; expires=Mon, 17-Mar-2014 04:47:09 GMT
Set-Cookie: first_login_flag=1; domain=renren.com; path=/
Set-Cookie: t=c5424876f4a3363b98b6f92e677f04bc2; domain=.renren.com; path=/
Set-Cookie: t=a0196d1d663ad5a060ee47466123042d; domain=renren.com; path=/xtalk/
Set-Cookie: societyguester=c5424876f4a3363b98b6f92e677f04bc2; domain=.renren.com; path=/
Set-Cookie: id=229996362; domain=.renren.com; path=/ (這個是返回的人人ID)
Set-Cookie: xnsid=cc216a6b; domain=.renren.com; path=/ (有這個就登陸成功了)
Set-Cookie: loginfrom=syshome; domain=.renren.com; path=/
#如下就是cookie了,之後發請求,就能夠帶上這個cookie
anonymid=hroelq3l-czxmdy; domain=.renren.com; path=/; expires=Thu, 14-Feb-2019 04:47:09 GMT, _de=97FB170A42B4342D1C47A157AD77AAFC1383380866D39FF5; domain=.renren.com; path=/; expires=Tue, 10-Feb-2015 04:47:09 GMT, p=31991a0a194c34e606ef1263317b06372; domain=renren.com; path=/; expires=Mon, 17-Mar-2014 04:47:09 GMT, ap=229996362; domain=renren.com; path=/; expires=Mon, 17-Mar-2014 04:47:09 GMT, first_login_flag=1; domain=renren.com; path=/, t=c5424876f4a3363b98b6f92e677f04bc2; domain=.renren.com; path=/, t=a0196d1d663ad5a060ee47466123042d; domain=renren.com; path=/xtalk/, societyguester=c5424876f4a3363b98b6f92e677f04bc2; domain=.renren.com; path=/, id=229996362; domain=.renren.com; path=/, xnsid=cc216a6b; domain=.renren.com; path=/, loginfrom=syshome; domain=.renren.com; path=/
後利用python作API測試作準備。
只講述模擬http的過程,具體到本身用的時候,要以本身的應用爲準作出適當的調整。