python - 连接中止 .', BadStatusLine("''",) 在服务器上?

标签 python python-3.x python-3.4

我使用以下代码从网络获取图像:

path = 'http://domgvozdem.ru/images/ustanovka-kondicionera-svoimi-rukami.jpg'
def exists(path):
    r = requests.head(path)
    return r.status_code == requests.codes.ok

我遇到了一个错误:

Main error has occurred: ('Connection aborted.', BadStatusLine("''",)) Main error has occurred: ('Connection aborted.', BadStatusLine("''",))

如何解决?它被主机阻止了吗?

最佳答案

当您的 Python 客户端收到空响应( header /正文)时,您会收到此错误。

顺便说一句,较新的 Python 版本会抛出不同的异常, 当服务器断开连接或网络问题时,可能会发生这种情况。

就我而言,我们花了数周时间尝试重现它,直到找到主要原因, 我们有一个 Python 应用程序向 Nginx 负载均衡器后面的服务发送请求,

我们发现当客户端超过Nginx默认配置的client_header_timeout/client_body_timeout(60秒)时,Nginx会断开连接,这是Nginx等待客户端额外数据包的时间。

你可以关注这个引用 http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_timeout

当我们的应用程序加载 CPU 处理时,连接已建立,但 header 和请求正文在传输之前有很长的延迟,超过 82 秒。

因此 Nginx 关闭连接并返回重置数据包(记录了带有空主体和 header 的 tcpdump),正式它应该返回状态码 408,这并没有发生。

我们通过将两个参数的 client_header_timeout/client_body_timeout 增加到 180 秒来解决这个问题:

server {
..
client_body_timeout 180s;
client_header_timeout 180s;
}

关于python - 连接中止 .', BadStatusLine("''",) 在服务器上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48814200/

相关文章:

python - 如何使用groupby创建新表并删除重复项

python - 运行时错误: Working outside of application context with Celery and Flask in Python

python - 如何将 YouTube API 持续时间转换为秒数?

python - Protobuf-2.6.0 与 Python 3?

python - Nohup 和 Python -u : it still doesn't log data in realtime

javascript - 在 Flask Web 应用程序中使用 JS 地理定位 (Python 3.6.6)

python-3.x - 使用 class_names 使用 graphviz 的树节点的颜色

python - arduino 读入 pyfirmata 给出的输出为 none

python - 连续调用时ElasticSearch不会返回匹配-Python

html - 用父元素的 beautifulsoup4 : does it affect the . 字符串解包元素?