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
axisray
V2EX  ›  Python

Python SimpleHTTPRequestHandler,如何保持 TCP 长连接

  •  
  •   axisray · 2016-11-27 23:03:38 +08:00 · 5825 次点击
    这是一个创建于 2699 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这个是用来模拟测试环境的,但是 302 那个 response 的 FIN 被置 1 ,连接断开了 如何在以下过程中保持 TCP 长连接? GET->302->GET

    from http.server import SimpleHTTPRequestHandler
    from http.server import HTTPServer
    
    #Listen Address
    ADDR = ''
    #Listen Port
    PORT = 80
    
    class WebRequestHandler(SimpleHTTPRequestHandler):
        def do_GET(self):
            #self.close_connection=False
            self.protocol_version='HTTP/1.1'
            rawpath =  self.path.split('?')[0]
            if(rawpath == '/ITPage/SurftheInternet.aspx' ):
                self.send_response(302)
                self.send_header('Content-Type','text/html; charset=utf-8')
                self.send_header('Location','/ITPage/SurftheInternet.html?openid=testestestestest')
                self.end_headers()
            if(rawpath == '/ITPage/SurftheInternet.html'):
                self.send_response(200)
                self.end_headers()
    server = HTTPServer((ADDR,PORT),WebRequestHandler)
    print("Server start!")
    server.serve_forever()
    
    第 1 条附言  ·  2016-11-30 14:03:08 +08:00
    问题已解决
    不需要 Connection:Keep-Alive , HTTP/1.1 默认就是长连接
    我遇到的问题是因为没有包含 Content-Length
    浏览器一直在等待数据,所以就卡死了
    添加 self.send_header('Content-Length','0') ,问题解决
    9 条回复    2016-12-03 16:06:34 +08:00
    ryd994
        1
    ryd994  
       2016-11-28 01:20:30 +08:00 via Android
    1. protocol version 直接放 class 里
    2. 设置 header Connection: keep-alive
    qq446015875
        2
    qq446015875  
       2016-11-28 09:54:46 +08:00 via iPhone
    @ryd994 我试了下,抓包确实看到了 302 没有 FIN ,但是浏览器似乎不认这个 302 ,浏览器 F12 没有记录到,也没有跳转
    如果只按 1 操作,抓包没有 302 , python 没有回应 GET
    qq446015875
        3
    qq446015875  
       2016-11-28 10:15:31 +08:00 via iPhone
    我好像知道了, Content-Length 吧?
    julyclyde
        4
    julyclyde  
       2016-11-28 11:01:00 +08:00
    你自己写 handler 啊
    那你输出的 header 里并没提到 Connection: Keep-Alive 啊
    ryd994
        5
    ryd994  
       2016-11-29 03:46:13 +08:00
    @qq446015875 不是 content-length
    你查查 Connection: keep-alive
    axisray
        6
    axisray  
    OP
       2016-11-30 13:59:54 +08:00
    @ryd994
    不需要 Connection: Keep-Alive ,浏览器在发起 GET 请求的时候就已经包含了 Connection: Keep-Alive
    而且 HTTP/1.1 默认就是长连接
    我遇到的问题是因为没有包含 Content-Length
    浏览器一直在等待数据,所以就卡死在哪了
    添加 self.send_header('Content-Length','0')
    这样就可以了
    ryd994
        7
    ryd994  
       2016-11-30 14:18:43 +08:00   ❤️ 1
    说什么都比不上源码: https://hg.python.org/cpython/file/3.6/Lib/http/server.py#l344
    你自己问的是连接断开了怎么办,怎么保持长连接
    请求 header 和响应 header 是两回事
    另外,你的问题文档里有明确说过: https://docs.python.org/3.7/library/http.server.html#http.server.BaseHTTPRequestHandler.protocol_version
    qq446015875
        8
    qq446015875  
       2016-12-03 15:58:06 +08:00 via iPhone
    @ryd994 嗯是我描述不清楚,我也是发完帖子注意到文档里说要加 content-length
    还是谢谢你了:)
    axisray
        9
    axisray  
    OP
       2016-12-03 16:06:34 +08:00 via iPhone
    我擦,没注意,暴露马甲了....
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2778 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 13:21 · PVG 21:21 · LAX 06:21 · JFK 09:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.