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

Python socket+asyncore 会出现连接重置问题

  •  
  •   holinhot · 2020-08-01 21:22:04 +08:00 · 1539 次点击
    这是一个创建于 1334 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 curl 测试的时候会出现连接重置的提示,大概 10 次必定出现一次或一次以上的重置 代码如下:

    import asyncore
    import socket
    class Echoer(asyncore.dispatcher_with_send):
        def handle_read(self):
            data = self.recv(1024)
            self.send("HTTP/1.1 200 OK\n")
    class EchoServer(asyncore.dispatcher):
        def __init__(self):
            asyncore.dispatcher.__init__(self)
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.bind(('', 4915))
            self.listen(1)
        def handle_accept(self):
            sock, addr = self.accept()
            handler = Echoer(sock)
    server = EchoServer()
    asyncore.loop()
    
    2 条回复    2020-08-04 00:24:20 +08:00
    sujin190
        1
    sujin190  
       2020-08-03 18:59:18 +08:00
    感觉是你返回的 HTTP 响应体不符合 HTTP 标准的问题导致的
    holinhot
        2
    holinhot  
    OP
       2020-08-04 00:24:20 +08:00
    @sujin190 同样内容不用 asyncore 没有问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3357 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 11:22 · PVG 19:22 · LAX 04:22 · JFK 07:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.