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-11-15 22:31:04 +00:00
|
|
|
from netlib import tcp, http_uastrings
|
2012-06-24 09:10:10 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2012-11-15 22:31:04 +00:00
|
|
|
preparser = argparse.ArgumentParser(add_help=False)
|
|
|
|
preparser.add_argument(
|
|
|
|
"--show-uas", dest="showua", action="store_true", default=False,
|
|
|
|
help="Print user agent shortcuts and exit."
|
|
|
|
)
|
|
|
|
pa = preparser.parse_known_args()[0]
|
|
|
|
if pa.showua:
|
|
|
|
print "User agent strings:"
|
|
|
|
for i in http_uastrings.UASTRINGS:
|
|
|
|
print " ", i[1], i[0]
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='A perverse HTTP client.', parents=[preparser])
|
2013-01-05 07:29:46 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-c", dest="connect_to", type=str, default=False,
|
|
|
|
metavar = "HOST:PORT",
|
|
|
|
help="Issue an HTTP CONNECT to connect to the specified host."
|
|
|
|
)
|
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-11-15 22:31:04 +00:00
|
|
|
|
2012-10-30 22:23:53 +00:00
|
|
|
group = parser.add_argument_group(
|
2012-11-15 22:31:04 +00:00
|
|
|
'Controlling Output',
|
2012-10-30 22:23:53 +00:00
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
2013-01-05 07:29:46 +00:00
|
|
|
if args.connect_to:
|
|
|
|
parts = args.connect_to.split(":")
|
|
|
|
if len(parts) != 2:
|
|
|
|
parser.error("Invalid CONNECT specification: %s"%args.connect_to)
|
|
|
|
try:
|
|
|
|
parts[1] = int(parts[1])
|
|
|
|
except ValueError:
|
|
|
|
parser.error("Invalid CONNECT specification: %s"%args.connect_to)
|
|
|
|
connect_to = parts
|
|
|
|
else:
|
|
|
|
connect_to = None
|
|
|
|
|
2012-07-22 00:49:59 +00:00
|
|
|
try:
|
|
|
|
for i in range(args.repeat):
|
2013-01-03 21:37:26 +00:00
|
|
|
p = pathoc.Pathoc(args.host, port, args.ssl, args.sni)
|
2012-07-22 00:49:59 +00:00
|
|
|
try:
|
2013-01-05 07:29:46 +00:00
|
|
|
p.connect(connect_to)
|
2012-07-22 00:49:59 +00:00
|
|
|
except tcp.NetLibError, v:
|
|
|
|
print >> sys.stderr, str(v)
|
|
|
|
sys.exit(1)
|
|
|
|
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
|
|
|
|
|