axisray
V2EX  ›  Python

Python SimpleHTTPRequestHandler,如何保持 TCP 长连接

By axisray at 2016 年 11 月 27 日 · 7394 次点击

这个是用来模拟测试环境的,但是 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 日
问题已解决
不需要 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 日 via Android
1. protocol version 直接放 class 里
2. 设置 header Connection: keep-alive
qq446015875
   2
qq446015875  
   2016 年 11 月 28 日 via iPhone
@ryd994 我试了下,抓包确实看到了 302 没有 FIN ,但是浏览器似乎不认这个 302 ,浏览器 F12 没有记录到,也没有跳转
如果只按 1 操作,抓包没有 302 , python 没有回应 GET
qq446015875
   3
qq446015875  
   2016 年 11 月 28 日 via iPhone
我好像知道了, Content-Length 吧?
julyclyde
   4
julyclyde  
   2016 年 11 月 28 日
你自己写 handler 啊
那你输出的 header 里并没提到 Connection: Keep-Alive 啊
ryd994
   5
ryd994  
   2016 年 11 月 29 日
@qq446015875 不是 content-length
你查查 Connection: keep-alive
axisray
   6
axisray  
OP
   2016 年 11 月 30 日
@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 日   ❤️ 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 月 3 日 via iPhone
@ryd994 嗯是我描述不清楚,我也是发完帖子注意到文档里说要加 content-length
还是谢谢你了:)
axisray
   9
axisray  
OP
   2016 年 12 月 3 日 via iPhone
我擦,没注意,暴露马甲了....
推荐学习书目
› 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
© 2026 V2EX · 37ms · 3.9.8.5