如何使用 Python 发出大型 HTTP(S) 请求?

JCGB

编辑:我将问题扩展到 HTTPS。“S”部分尚未解决,我并不真正需要它,但对其他人来说可能很有趣。

我想做相当于

hdr='GET / HTTP/1.1\r\nContent-Length: 10000000000\r\n\r\n'
(echo -en "$hdr"; dd if=/dev/zero bs=1000000 count=999; read tmp) | nc $SOME_IP 80

使用 Python 2.7。如果可能,我只想使用标准库加上requestssockets模块。

仅供参考,上面的脚本会发送一个大型 HTTP 请求(约 1GB 的零),$SOME_IP而不会占​​用发件人 RAM。

怪异的

像这样的东西?

import socket

def get_content_length():
    return 10000000000

def get_chunk_size():
    return 1000000

def yield_content():
    total_size = get_content_length()
    chunk_size = get_chunk_size()
    current_size = 0
    end = total_size - chunk_size
    chunk = '\x00' * chunk_size
    while current_size < end:
        yield chunk
        current_size += chunk_size
    yield chunk[:total_size - current_size]

s = socket.socket()
s.connect((host, port))
hdr='GET / HTTP/1.1\r\nContent-Length: %s\r\n\r\n' % get_content_length()
s.sendall(hdr)
for chunk in yield_content():
    s.sendall(chunk)
# should I wait for the response?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章