HTTP 1.1 Expect header handshake

2019-07-19 12:04发布

According http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3 client has to wait for 100 (Continue) status, before making POST (on HTTP 1.1 server that requires this header).

I can't understand how can Python do this. For example:

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

Now, I don't see other options then send the data:

conn.send(data)

in which case I get error, when asking for response:

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

How can I ask for status 100, so that I can send the data?

标签: python http
1条回答
小情绪 Triste *
2楼-- · 2019-07-19 12:45

I don't think httplib supports this (very well); see the discussion in this requests ticket, specifically

and can close the socket early in a way that breaks httplib

The author of that comment, Augie Fackler (durin42) also mentions his own httpplus library.

A quick glance at the source code shows promise that it does handle Expect: 100-Continue correctly:

# 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
查看更多
登录 后发表回答