如何通过python的httplib2.Http请求方法发布超过64K的数据?

发布于 2024-11-15 18:54:14 字数 488 浏览 2 评论 0原文

如果我这样做:

h = httplib2.Http(timeout=60)

resp, content = h.request(uri, method=method, body=body, headers=headers,
                          redirections=redirections,
                          connection_type=connection_type)

如果正文超过 64K,则数据似乎被截断。

这是在 32 位 Python 运行时上发生的(我认为 64 位运行时不会发生这种情况)。

我该如何实现这个目标?

问题如下:

https://svn.macports.org/ticket/18376

If I do this:

h = httplib2.Http(timeout=60)

resp, content = h.request(uri, method=method, body=body, headers=headers,
                          redirections=redirections,
                          connection_type=connection_type)

If body is more than 64K the data appears to be truncated.

This is on a 32-bit Python runtime (I don't think it happens with a 64-bit runttime).

How do I accomplish this?

Here's the issue:

https://svn.macports.org/ticket/18376

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂归处 2024-11-22 18:54:14

我第一次听说这个。这是一个简短的程序,表明这对我有用。请注意,我运行的 full.cgi 脚本仅将请求标头和请求正文返回到响应正文中。当我运行这个完整的 64K+ 内容时,包括“结束”往返。

import httplib2

h = httplib2.Http(timeout=60)
body = "x"* (64*1024) + " the end"
uri="http://bitworking.org/projects/httplib2/test/reflector/full.cgi"
resp, content = h.request(uri, method="POST", body=body)
print content

您确定在wireshark 中看到的不仅仅是TCP 段吗?这些将被截断为小于 64K,但不代表完整的 HTTP 请求。

First time I've ever heard of this. Here is a short program to show that this works for me. Note that the full.cgi script I have running just returns the request headers and request body back in the response body. When I run this the full 64K+ of content, including the ' the end' roundtrips.

import httplib2

h = httplib2.Http(timeout=60)
body = "x"* (64*1024) + " the end"
uri="http://bitworking.org/projects/httplib2/test/reflector/full.cgi"
resp, content = h.request(uri, method="POST", body=body)
print content

Are you sure you aren't seeing just the TCP segements in wireshark? Those would be truncated to less than 64K, but don't represent the full HTTP request.

可爱咩 2024-11-22 18:54:14

该问题与未打补丁的 2.6.6 版本有关。换句话说,它与后来修复的已知错误有关。显然我得到了一个不包含此修复的 python 版本。

有关此问题的更多信息,请参阅此帖子:https://svn.macports.org/ticket/18376< /a>

修补版本设置 HAVE_POLL=0,强制 python 使用 select 代替。确保您使用的 python 版本包含此补丁,否则推送较大的数据块将会挂起。

另一个解决方案是重写 httplib.py 的 send 方法以捕获“35”异常并重新发送数据。

下面是一些代码来说明这一点:

            blen = len(str)
            bleft = len(str)
            bpos = 0
            bsize = 1024*8
            while bleft > 0:
                bend = bpos + bsize
                if bend >= blen:
                    bend = blen
                try:
                    slen = self.sock.send(str[bpos:bend])
                except socket.error, v:
                    if v.args[0] == 35:      # unavailable
                        #print('socket unavailable')
                        slen = 0
                        time.sleep(.5)
                    else:
                        raise
                bleft -= slen
                bpos += slen

代替 self.sock.sendall

The problem related to an unpatched build of 2.6.6. In other words, it relates to a known bug that was later fixed. Apparently I got a build of python that did not include this fix.

Please see this post for more info on this problem: https://svn.macports.org/ticket/18376

The patched version set HAVE_POLL=0, forcing python to use select instead. Make sure you are using a version of python that includes this patch, or pushing larger blocks of data will hang.

Another solution is a rewrite of the httplib.py's send method to catch the '35' exception and resend the data.

Here is some code that illustrates this:

            blen = len(str)
            bleft = len(str)
            bpos = 0
            bsize = 1024*8
            while bleft > 0:
                bend = bpos + bsize
                if bend >= blen:
                    bend = blen
                try:
                    slen = self.sock.send(str[bpos:bend])
                except socket.error, v:
                    if v.args[0] == 35:      # unavailable
                        #print('socket unavailable')
                        slen = 0
                        time.sleep(.5)
                    else:
                        raise
                bleft -= slen
                bpos += slen

in place of self.sock.sendall

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文