如何使用Python提出大型HTTP请求



编辑:我将问题扩展到https。" s"部分尚未解决,我真的不需要它,但这可能对其他人来说很有趣。

我想做等效的

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

使用Python 2.7。如果可能的话,我想仅使用标准库以及requestssockets模块。

fyi,上面的脚本将大型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.1rnContent-Length: %srnrn' % get_content_length()
s.sendall(hdr)
for chunk in yield_content():
    s.sendall(chunk)
# should I wait for the response?

这是我煮熟并似乎有效的东西:

import sys, socket
def flood(hostname, port, count=1):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((hostname, port))
    s.sendall('GET / HTTP/1.1rnContent-Length: 10000000000rnrn')
    with open('/dev/zero', 'rb', ) as data:
        for _ in xrange(count):
            s.sendall(data.read(1000000))
    s.shutdown(socket.SHUT_WR)
    while True:
        data = s.recv(1024)
        if data == "":
            break
        print("Received:", repr(data))
    print("Connection closed.")
    s.close()

Freakish的答案当然更跨平台,因为它不需要/dev/null

最新更新