python 및 머신러닝 교육, 슬로우캠퍼스


HTTP redirect 처리하기

curl 명령어로 redirect가 발생하는 지 확인할 수 있다.  --head 옵션을 사용한다.

curl --head  http://j.mp/174gpKP

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 05 Jun 2014 04:21:44 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Cache-Control: private; max-age=90
Content-Length: 153
Location: http://nolboo.github.io/blog/2013/10/17/start-blog-with-harp/
Mime-Version: 1.0
Set-Cookie: _bit=538ff058-00317-06caf-3b1cf10a;domain=.j.mp;expires=Tue Dec  2 04:21:44 2014;path=/; HttpOnly



http://j.mp/174gpKP 이라는 short URL의 원본 URL을 알고 싶다면

HTTP 301 redirect를 처리할 수 있어야 한다.  (302, 303 등도 있음)



>>> import urllib

>>> a = urllib.urlopen("http://j.mp/174gpKP")

>>> a.geturl()
'http://nolboo.github.io/blog/2013/10/17/start-blog-with-harp/'


urllib2를 이용한 방법도 있지만, 조금 복잡하다. class 정의 필요.

http://www.diveintopython.net/http_web_services/redirects.html




'Data/Text/Knowledge Analysis & Mining > Python' 카테고리의 다른 글

mechanize 예시  (0) 2013.10.18
[Git] 기본 설정 및 사용  (0) 2013.07.30
python pdf - reportlab  (0) 2013.07.26
OCR + python  (0) 2013.07.26
python pdf library 비교  (0) 2013.07.26

WRITTEN BY
manager@
Data Analysis, Text/Knowledge Mining, Python, Cloud Computing, Platform

,