問題引入
前段時間做資料收集時需要下載網宿cdn的日志進行分析,而網宿對日志下載的介面搞得很復雜,又沒有提供相應的sdk,只是提供了一個shell腳本,雖然在ubuntu上使用很方便,但是腳本里面的各種重定向分析非常復雜,故此想對重定向在深入了解一點,
查詢網宿日志串列的腳本
#!/bin/sh
TMP_FILE="/tmp/wslog_query_client.log"
#Usage
Usage() {
echo "wslog_query_client.sh [query_url] [user] [passwd] [start_time] [end_time] [channels]"
return 0
}
#check input parameters
if [ $# -eq 1 ]; then
if [ "$1" = "-h" ]; then
Usage
exit 0
else
Usage
exit -1
fi
elif [ $# -ne 6 ]; then
Usage
exit -1
fi
#params set
url=$1
user=$2
passwd=`echo $3 | sed 's/&/%26/g' `
start_time=$4
end_time=$5
channels=$6
#access logQuery access API
curl -s -D $TMP_FILE $1
cat $TMP_FILE | grep "HTTP/" | grep "302" > /dev/null
if [ $? -ne 0 ]; then
exit -2
fi
#redirect to verify url with user and passwd
TMP_URL=`cat $TMP_FILE | grep "Location: "|sed 's/\r//' | awk '{print $2}' | sed 's/http:/https:/'`
TMP_URL="${TMP_URL}?u=$user&p=$passwd&channel=$channels"
curl -s -k -D $TMP_FILE $TMP_URL
cat $TMP_FILE | grep "HTTP/" | grep "302" > /dev/null
if [ $? -ne 0 ]; then
exit -3
fi
#redirect to query url with start_time, end_time and channels
TMP_URL=`cat $TMP_FILE | grep "Location: "|sed 's/\r//' | awk '{print $2}'`
TMP_URL="${TMP_URL}&start_time=$start_time&end_time=$end_time&channels=$channels"
curl -s -D $TMP_FILE $TMP_URL
#check query result
cat $TMP_FILE | grep "HTTP/" | grep "200" > /dev/null
if [ $? -ne 0 ]; then
if
cat $TMP_FILE | grep "HTTP/" | grep "404" > /dev/null
then
exit -404
else
exit -4
fi
fi
exit 0
腳本呼叫命令和結果(用戶名,密碼,domain,wskey均已處理,呼叫結果只有參考作用)
root@sz3:/tmp# sh /root/wslog_query_client.sh "http://dx.wslog.chinanetcenter.com/logQuery/access" user1 passwd1 2017-08-30-0000 2017-08-30-2359 "rtmp-wsz.enterprise.com"
{"logs": [{"domain": "rtmp-wsz.enterprise.com", "files": [{"size": 4320, "end_time": "2017-08-30-1159", "start_time": "2017-08-30-0000", "url": "http://dx.wslog.chinanetcenter.com/log/qukan/rtmp-wsz.enterprise.com/2017-08-30-0000-1130_rtmp-wsz.enterprise.com.cn.log.gz?wskey=e4030060bdfe9d5600a77726c5900d07aa3adae00e8b2"}, {"size": 8006, "end_time": "2017-08-30-2359", "start_time": "2017-08-30-1200", "url": "http://dx.wslog.chinanetcenter.com/log/qukan/rtmp-wsz.enterprise.com/2017-08-30-1200-2330_rtmp-wsz.enterprise.com.cn.log.gz?wskey=3772006094880e8300a73cc2c59006bfeea33ae00d9da"}]}]}
腳本的呼叫程序是根據引數一步一步的進行302重定向,重定向時會依賴于引數,每次重定向依賴的引數都不相同,不僅僅是url跳轉,如果直接使用以下http鏈接則無法跳轉到,因此需要按照shell腳本那樣一層一層決議,
http://dx.wslog.chinanetcenter.com/logQuery/access?user=user1&passwd=passwd1&channels=rtmp-wsz.enterprise.com&start_time=2017-08-30-0000&end_time=2017-08-30-2359
HTTP重定向的原理

客戶端發起http請求,如果服務端回傳http重定向回應,那么客戶端會請求回傳的新url,這就是重定向的程序,這個程序就是重定向,在客戶端和服務端之間自動完成,用戶不可見,
不同型別的重定向映射可以劃分為三個類別:永久重定向、臨時重定向和特殊重定向,
如果你想把自己的網站永久更改為一個新的域名,則應該使用301永久重定向,搜索引擎機器人會在遇到該狀態碼時觸發更新操作,在其索引庫中修改與該資源相關的 URL ,
HTTP重定向的使用
主要以Python和shell兩種語言來介紹http重定向的使用,
Python
Python常用的http庫urllib,urllib2,requests都支持http重定向,以requests庫為例做介紹,
import requests
def get_final_link(url):
try:
r = requests.get(url=url, allow_redirects=False)
if r.status_code == 302 or r.status_code == 301:
return get_final_link(r.headers['Location'])
else:
return r.url
except:
return url
def get_final_link1(url):
r = requests.get(url=url, allow_redirects=True)
for rsp in r.history:
print rsp.url
return r.url
print get_final_link(url='http://runreport.dnion.com/DCC/logDownLoad.do?user=user1&password=password1&domain=rtmpdist-d.quklive.com&date=20171026&hour=10')
print get_final_link(url='https://github.com')
print get_final_link(url='http://github.com')
print get_final_link1(url='http://github.com') # 會發生301重定向
如果確定重定向的程序中全部都是http(s)請求,則allow_redirects引數設定成True即可得到最終的http鏈接,如果不是則需要自己進行遞回決議,
如果是要簡單的下載檔案,則可以使用urllib.urlretrieve輕松勝任,即使最終鏈接是ftp,
Shell
使用curl命令模擬
-L引數,當頁面有跳轉的時候,輸出跳轉到的頁面
-I引數 header資訊 當有跳轉時,可以通過 curl -L -I URL|grep Location 來確定跳轉到的新url地址
root@sz3:~# curl -L -I "http://runreport.dnion.com/DCC/logDownLoad.do?user=user1&password=password1&domain=rtmpdist-d.quklive.com&date=20171026&hour=10"
HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=0F11668F6EBF4DC16B43E322CCF16C85; Path=/DCC
Location: http://runreport.dnion.com/logDownLoad.do?user=qukan&password=0cddcbf6d292fab5de0aas931bf19c&domain=rtmpdist-d.quklive.com&date=20171026&hour=10
Content-Type: text/html;charset=GBK
Content-Length: 0
Date: Mon, 06 Nov 2017 09:46:44 GMT
HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Location: ftp://ABA606843D412DAE34F28CDB23F7A31E:0687B16F2F5D0A2637FACDB23FAC982179411FA7466F10B2E7D0F4AA2D7F6AD42536F122549D0A6E40337E896@125.39.237.48:55621/rtmpdist-d.quklive.com_20171026_10_11.gz
Content-Type: text/html;charset=GBK
Content-Length: 0
Date: Mon, 06 Nov 2017 09:46:44 GMT
Last-Modified: Thu, 26 Oct 2017 02:30:13 GMT
Content-Length: 1932
Accept-ranges: bytes
最后跳轉到需要的ftp鏈接,
HTTP重定向抓包驗證
使用wireshark抓包結果如下:
第一次跳轉程序如下圖

第二次跳轉程序如下圖

所以通過抓包可以清晰的看到302跳轉的程序
參考:
- csdn-curl命令的常見引數使用
- mozilla-HTTP 的重定向
記得幫我點贊哦!
精心整理了計算機各個方向的從入門、進階、實戰的視頻課程和電子書,按照目錄合理分類,總能找到你需要的學習資料,還在等什么?快去關注下載吧!!!

念念不忘,必有回響,小伙伴們幫我點個贊吧,非常感謝,
我是職場亮哥,YY高級軟體工程師、四年作業經驗,拒絕咸魚爭當龍頭的斜杠程式員,
聽我說,進步多,程式人生一把梭
如果有幸能幫到你,請幫我點個【贊】,給個關注,如果能順帶評論給個鼓勵,將不勝感激,
職場亮哥文章串列:更多文章

本人所有文章、回答都與著作權保護平臺有合作,著作權歸職場亮哥所有,未經授權,轉載必究!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/232295.html
標籤:Python
下一篇:python實戰筆記(一)
