python - 将超时设置为python套接字连接recv方法
问题描述
我正在尝试编写一个简单的 http 服务器。我希望它对Connection: keep-alive
标头起作用,所以我想确保它connection.recv()
会超时。我已经尝试过socket.settimeout(timeout)
方法,但timeout
即使收到数据,它也会在一段时间后关闭连接。我想要的是这样的:
while True:
try:
#start to wait for timeout seconds
request = conn.recv(256, timeout)
process()
#if there is no keep-alive header end the operation
if "keep-alive" not in request:
conn.close()
break
else:
#if there was a keep alive header start getting the data
continue
except:
#this means timeout seconds passed after recv() was called
conn.close()
break
最好的方法是什么?
解决方案
推荐阅读
- r - 如何通过两个因素进行计算
- python - .agg PYTHON 中未定义均值
- php - 递归地将值添加到数组
- amazon-web-services - 在 GitHub 存储库中的每个新拉取请求上触发 AWS CodePipeline
- sql - 有没有办法根据条件退出当前功能但不结束程序
- java - 使用 JsonArray 和 HashMap 解析 JSON
- ios - tableView 单元格中的 UI 按钮后的“A”字符?
- r - export::graph2office 移动轴标签
- swiftui - SwiftUI 列表背景颜色
- amazon-web-services - CloudWatch 代理 CPU 收集的指标未显示