2012-06-24 09:10:10 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import argparse, sys
|
2012-10-04 21:30:32 +00:00
|
|
|
from libpathod import pathoc, version, language
|
2012-06-29 22:51:13 +00:00
|
|
|
from netlib import tcp
|
2012-06-24 09:10:10 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description='A perverse HTTP client.')
|
2012-07-21 04:19:44 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-i", dest="sni", type=str, default=False,
|
2012-09-26 21:44:25 +00:00
|
|
|
help="SSL Server Name Indication"
|
2012-07-21 04:19:44 +00:00
|
|
|
)
|
2012-07-21 03:15:10 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-n", dest='repeat', default=1, type=int, metavar="N",
|
2012-09-26 21:44:25 +00:00
|
|
|
help='Repeat requests N times'
|
2012-07-21 03:15:10 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p", dest="port", type=int, default=None,
|
2012-09-26 21:44:25 +00:00
|
|
|
help="Port. Defaults to 80, or 443 if SSL is active"
|
2012-07-21 03:15:10 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-s", dest="ssl", action="store_true", default=False,
|
2012-09-26 21:44:25 +00:00
|
|
|
help="Connect with SSL"
|
2012-07-21 03:15:10 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2012-07-21 04:19:44 +00:00
|
|
|
"-t", dest="timeout", type=int, default=None,
|
2012-09-26 21:44:25 +00:00
|
|
|
help="Connection timeout"
|
2012-07-21 03:15:10 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'host', type=str,
|
|
|
|
help='Host to connect to'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'request', type=str, nargs="+",
|
|
|
|
help='Request specification'
|
|
|
|
)
|
2012-10-30 22:23:53 +00:00
|
|
|
group = parser.add_argument_group(
|
|
|
|
'Controlling Output',
|
|
|
|
"""
|
|
|
|
Some of these options expand generated values for logging - if
|
|
|
|
you're generating large data, use them with caution.
|
|
|
|
"""
|
|
|
|
)
|
2012-09-26 21:44:25 +00:00
|
|
|
group.add_argument(
|
2012-09-27 23:38:49 +00:00
|
|
|
"-C", dest="ignorecodes", type=str, default="",
|
2012-09-26 22:56:06 +00:00
|
|
|
help="Comma-separated list of response codes to ignore"
|
2012-09-26 21:44:25 +00:00
|
|
|
)
|
2012-09-25 22:12:30 +00:00
|
|
|
group.add_argument(
|
|
|
|
"-e", dest="explain", action="store_true", default=False,
|
2012-09-26 21:44:25 +00:00
|
|
|
help="Explain requests"
|
2012-09-25 22:12:30 +00:00
|
|
|
)
|
2012-09-26 22:56:06 +00:00
|
|
|
group.add_argument(
|
|
|
|
"-o", dest="oneshot", action="store_true", default=False,
|
|
|
|
help="Oneshot - exit after first non-ignored response"
|
|
|
|
)
|
2012-09-25 22:12:30 +00:00
|
|
|
group.add_argument(
|
|
|
|
"-q", dest="showreq", action="store_true", default=False,
|
2012-09-26 21:44:25 +00:00
|
|
|
help="Print full request"
|
2012-09-25 22:12:30 +00:00
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
"-r", dest="showresp", action="store_true", default=False,
|
2012-09-26 21:44:25 +00:00
|
|
|
help="Print full response"
|
2012-09-25 22:12:30 +00:00
|
|
|
)
|
2012-09-27 23:38:49 +00:00
|
|
|
group.add_argument(
|
|
|
|
"-T", dest="ignoretimeout", action="store_true", default=False,
|
|
|
|
help="Ignore timeouts"
|
|
|
|
)
|
2012-09-25 22:12:30 +00:00
|
|
|
group.add_argument(
|
|
|
|
"-x", dest="hexdump", action="store_true", default=False,
|
2012-09-26 21:44:25 +00:00
|
|
|
help="Output in hexdump format"
|
2012-09-25 22:12:30 +00:00
|
|
|
)
|
2012-06-24 09:10:10 +00:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
2012-06-24 09:40:31 +00:00
|
|
|
|
2012-06-24 09:10:10 +00:00
|
|
|
if args.port is None:
|
|
|
|
port = 443 if args.ssl else 80
|
|
|
|
else:
|
|
|
|
port = args.port
|
|
|
|
|
2012-09-26 21:44:25 +00:00
|
|
|
try:
|
|
|
|
codes = [int(i) for i in args.ignorecodes.split(",") if i]
|
|
|
|
except ValueError:
|
|
|
|
parser.error("Invalid return code specification: %s"%args.ignorecodes)
|
|
|
|
|
2012-07-22 00:49:59 +00:00
|
|
|
try:
|
|
|
|
for i in range(args.repeat):
|
|
|
|
p = pathoc.Pathoc(args.host, port)
|
|
|
|
try:
|
|
|
|
p.connect()
|
|
|
|
except tcp.NetLibError, v:
|
|
|
|
print >> sys.stderr, str(v)
|
|
|
|
sys.exit(1)
|
|
|
|
if args.ssl:
|
|
|
|
try:
|
|
|
|
p.convert_to_ssl(sni=args.sni)
|
|
|
|
except tcp.NetLibError, v:
|
|
|
|
print "\n>> %s"%v
|
|
|
|
continue
|
|
|
|
if args.timeout:
|
|
|
|
p.settimeout(args.timeout)
|
2012-09-26 02:25:39 +00:00
|
|
|
for spec in args.request:
|
2012-09-26 22:56:06 +00:00
|
|
|
ret = p.print_request(
|
2012-09-26 02:25:39 +00:00
|
|
|
spec,
|
|
|
|
showreq=args.showreq,
|
|
|
|
showresp=args.showresp,
|
|
|
|
explain=args.explain,
|
|
|
|
hexdump=args.hexdump,
|
2012-09-27 23:38:49 +00:00
|
|
|
ignorecodes=codes,
|
|
|
|
ignoretimeout=args.ignoretimeout
|
2012-09-26 02:25:39 +00:00
|
|
|
)
|
|
|
|
sys.stdout.flush()
|
2012-09-26 22:56:06 +00:00
|
|
|
if ret and args.oneshot:
|
|
|
|
sys.exit(0)
|
2012-07-22 00:49:59 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|