Last active
January 4, 2016 02:59
-
-
Save kshcherban/8558691 to your computer and use it in GitHub Desktop.
Ammo generator from file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import argparse | |
import os | |
import stat | |
import sys | |
def gen_request(req_type, url, data, hostname, tag): | |
full_request='''{0} {1}{2} HTTP/1.1\r | |
User-Agent: YandexTank/1.1.1\r | |
Host: {3}\r | |
Accept-Encoding: gzip, deflate\r | |
Connection: Close\r\n\r\n'''.format(req_type, url, data, hostname) | |
return '''{0} {1}\n{2}'''.format(len(full_request), tag, full_request) | |
if __name__ == '__main__': | |
# Parsing arguments | |
parser = argparse.ArgumentParser(prog='yatank-req.py') | |
parser.add_argument('-t', '--type', type=str, dest='req', default='GET', | |
choices=['GET', 'POST'], | |
help='request type, can be GET or POST, default: "GET"') | |
parser.add_argument('--tag', type=str, dest='tag', default='tag', | |
help='request tag, default: tag') | |
parser.add_argument('-u', '--url', type=str, default='/', | |
help='url, should begin with "/", default: "/"') | |
parser.add_argument('--host', type=str, default="localhost", | |
help='hostname of target, default: "localhost"') | |
# Detect if reading from stdin | |
mode = os.fstat(0).st_mode | |
# If piped or redirected | |
if stat.S_ISFIFO(mode) or stat.S_ISREG(mode): | |
args = parser.parse_args() | |
for line in sys.stdin: | |
print gen_request(args.req, args.url.strip(), line.strip(), | |
args.host.strip(), args.tag) | |
else: | |
parser.add_argument('-f', '--file', type=argparse.FileType('r'), dest='file', | |
help='file with requests', required=True) | |
args = parser.parse_args() | |
for i in args.file: | |
print gen_request(args.req, args.url.strip(), i.strip(), | |
args.host.strip(), args.tag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment