推荐学习书目
› 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 · Aug 1, 2020 · 2394 views
    This topic created in 2247 days ago, the information mentioned may be changed or developed.

    使用 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 replies  •  2020-08-04 00:24:20 +08:00
    sujin190
        1
    sujin190  
       Aug 3, 2020
    感觉是你返回的 HTTP 响应体不符合 HTTP 标准的问题导致的
    holinhot
        2
    holinhot  
    OP
       Aug 4, 2020
    @sujin190 同样内容不用 asyncore 没有问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2714 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 14:51 · PVG 22:51 · LAX 07:51 · JFK 10:51
    ♥ Do have faith in what you're doing.