V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yinshunyao
V2EX  ›  Python

求助, httplib 遇到重定向怎么处理, Location 我取出来了,但是新的 GET 发不出去了。

  •  
  •   yinshunyao · 2016-06-18 00:08:59 +08:00 · 1912 次点击
    这是一个创建于 2880 天前的主题,其中的信息可能已经有所发展或是发生改变。
    尝试了关闭当前的 HTTPConnection ,新开一个,也还是 GET 不到。抓包 GET 发不出去。初始化成功了的。没思路了

    status,response = 1,''
    try:
    start = time.time()
    if connDefault is None:
    conn = HTTPConnection(self.host,timeout=60)
    else:
    conn = connDefault

    self.headers["Content-Length"] = len(PostStr)
    conn.request('POST',url,PostStr,headers=self.headers)
    response = conn.getresponse()
    end = time.time()
    print "http_post info:",end - start,response.status

    #重定向
    if response.status == 302:
    Location=response.getheader('Location','')
    #print "http_post 302 body:",response.read()
    #print 'cd_chose.aspx enter the redirect',response
    print conn.request('GET',Location,headers=self.headers)
    response = conn.getresponse()
    status,response = 302,Location
    elif response.status == 200:
    status,response = 200,''
    else:
    status,response = response.status,'does not support'
    except Exception,ex:
    print 'http_post -> error:',ex
    status,response = 1,ex
    finally:
    if connDefault is None:
    conn.close()
    return status,response
    yinshunyao
        1
    yinshunyao  
    OP
       2016-06-18 18:56:19 +08:00
    搞定了,问题想复杂了, http 基础太薄弱,重定向没有什么问题,只是我的 http get 头错了,多了一个字段,服务器校验太严格。

    http://www.cnblogs.com/inns/p/5596689.html
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1827 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:00 · PVG 08:00 · LAX 17:00 · JFK 20:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.