Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决timeout #5

Open
TitorX opened this issue Oct 20, 2015 · 3 comments
Open

解决timeout #5

TitorX opened this issue Oct 20, 2015 · 3 comments
Labels

Comments

@TitorX
Copy link
Owner

TitorX commented Oct 20, 2015

某些时候服务器与浏览器建立的连接没有及时断开。会出现timeout异常。

@TitorX
Copy link
Owner Author

TitorX commented Oct 25, 2015

初步分析。问题在于套接字recv阶段。
目前套接字层采用循环recv的方法

while True:
    data = recv(1024)
    if len(data) < 1024:
        break    

选择该模式是由于无法准确预测请求的长度。因此循环recv
而当最后一次recv块的大小刚好等于一次recv的字节数时。则不会进入break分支
再次recv时,由于套接字连接未中断而等待接受的数据为0
程序就停留在recv处不再执行
等待套接字超时后就会抛出timeout异常

解决方案是寻找合适的方式预测套接字数据是否接受完毕。

@TitorX
Copy link
Owner Author

TitorX commented Nov 1, 2017

暂时没有解决思路

@TitorX TitorX assigned TitorX and unassigned TitorX Nov 5, 2017
@EddieIvan01
Copy link

EddieIvan01 commented Jan 19, 2020

socket的recv只是从本地TCP协议栈拷贝数据,recv(1024)只能保证数据不大于1024,应该针对应用层协议对TCP流分块

buf = []
cl = 0

while True:
    data = recv(1)
    buf.append(data)
    if buf[-4:] == b'\r\n\r\n':
        break
    if current_req_header() == 'Content-Length':
        cl= parse_content_length()

buf.append(recv(cl))
close_conn()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants