HTTP 1.1 Expect标头握手

时间:2013-03-22 11:48:41

标签: python http

根据http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3客户端必须等待100(继续)状态,然后才能进行POST(在需要此标头的HTTP 1.1服务器上)。

我无法理解Python如何做到这一点。例如:

conn = httplib.HTTPConnection(url)
conn.putrequest('POST', page)
conn.putheader(...)
conn.putheader('Expect', '100-continue')
conn.endheaders()

现在,我没有看到其他选项然后发送数据:

conn.send(data)

在这种情况下,当我要求回复时我得到错误:

error: [Errno 10053] An established connection was aborted by the software in your host machine

如何申请状态100,以便我可以发送数据?

1 个答案:

答案 0 :(得分:3)

我认为httplib不支持这个(非常好);请参阅this requests ticket中的讨论,特别是

  

并且可以以破坏httplib

的方式提前关闭套接字

该评论的作者Augie Fackler(durin42)也提到了他自己的httpplus library

快速浏览一下source code,可以看出 it 正确处理Expect: 100-Continue

# handle 100-continue response
hdrs, body = self.raw_response.split(self._end_headers, 1)
http_ver, status = hdrs.split(' ', 1)
if status.startswith('100'):
    self.raw_response = body
    self.continued = True
    logger.debug('continue seen, setting body to %r', body)
    return