推荐学习书目
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 · Jun 18, 2016 · 2298 views
    This topic created in 3688 days ago, the information mentioned may be changed or developed.
    尝试了关闭当前的 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
       Jun 18, 2016
    搞定了,问题想复杂了, http 基础太薄弱,重定向没有什么问题,只是我的 http get 头错了,多了一个字段,服务器校验太严格。

    http://www.cnblogs.com/inns/p/5596689.html
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1165 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:23 · PVG 07:23 · LAX 16:23 · JFK 19:23
    ♥ Do have faith in what you're doing.