test url redirection

first we need similarate a redirect server enviroment,we use bottle a simple single python wsgi server to support this case

 

from bottle import route, run,redirect
import time
@route('/hello/:name')
def index(name='World'):
   
    r=int(name)+1
    time.sleep(1)
    if r<4:
        redirect("/hello/"+str(r),code=302)
    else:
        return '<b>Hello %s!</b>' % name

run(host='localhost', port=8080)

 

then we use curl to test is OK

curl -iL --max-redirs 200 http://localhost:8080/hello/1

 

finally we can use urllib2.urlopen(url).read() to do our redirect handler

 

 

#!/usr/bin/env python
#encoding=utf-8
import urllib2
url="http://detail.tmall.com/item.htm?id=13283399422&show_review=1"
url="http://localhost:8080/hello/1"
#curl -iL --max-redirs 200 http://localhost:8080/hello/1

#print urllib2.urlopen(url).read()

class MyHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
    def __init__(self):
        self.max_repeats=200
        self.max_redirections=200
        print self.max_repeats
        print self.max_redirections
       
request = urllib2.Request(url)
import httplib
httplib.HTTPConnection.debuglevel = 1
opener = urllib2.build_opener(MyHTTPRedirectHandler())
f = opener.open(request)
print f.read()

 

reference:

http://woodpecker.org.cn/diveintopython/http_web_services/redirects.html

你可能感兴趣的:(redirect)