zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    同一个硬盘安装win10+ubuntu双系统
    Bundle
    layout_weight属性
    Java反射机制
    Android——Widget实现
    Android——悬浮窗+侧边弹框+淡入淡出+背景shape+SeekBar调节手机亮度
    Android权限总结
    Android——窗口层控制WindowManager.LayoutParams.type
    Android——getSystemService
    Eclipse UML插件——AmaterasUML
  • 原文地址:https://www.cnblogs.com/lexus/p/2437552.html
Copyright © 2011-2022 走看看