From a747043cc81bd6413b6c85b2517530caf38e8a54 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 24 Jun 2012 22:59:20 +1200 Subject: [PATCH] Use http.read_response to read server response. Also add a few different output formats, and a verbosity flag. --- libpathod/pathoc.py | 14 +++++++++++--- pathoc | 15 +++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index e378e8632..c2abcaeb1 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -4,6 +4,16 @@ import rparse class PathocError(Exception): pass +def print_short(fp, httpversion, code, msg, headers, content): + print >> fp, "%s %s: %s bytes"%(code, msg, len(content)) + + +def print_full(fp, httpversion, code, msg, headers, content): + print >> fp, "HTTP%s/%s %s %s"%(httpversion[0], httpversion[1], code, msg) + print >> fp, headers + print >> fp, content + + class Pathoc(tcp.TCPClient): def __init__(self, ssl, host, port, clientcert): try: @@ -15,7 +25,5 @@ class Pathoc(tcp.TCPClient): r = rparse.parse_request({}, spec) r.serve(self.wfile) self.wfile.flush() - - line = self.rfile.readline() - print line + return http.read_response(self.rfile, r.method, None) diff --git a/pathoc b/pathoc index 0115516bb..e4fee2db6 100755 --- a/pathoc +++ b/pathoc @@ -4,8 +4,9 @@ from libpathod import pathoc, version if __name__ == "__main__": parser = argparse.ArgumentParser(description='A perverse HTTP client.') - parser.add_argument('--port', type=int, default=None, help="Port. Defaults to 80, or 443 if SSL is active.") - parser.add_argument('--ssl', action="store_true", default=False, help="Connect with SSL.") + parser.add_argument('--port', "-p", type=int, default=None, help="Port. Defaults to 80, or 443 if SSL is active.") + parser.add_argument('--ssl', "-s", action="store_true", default=False, help="Connect with SSL.") + parser.add_argument('--verbose', '-v', action='count') parser.add_argument('host', type=str, help='Host to connect to') parser.add_argument('request', type=str, nargs="+", help='Request specification') @@ -16,10 +17,16 @@ if __name__ == "__main__": else: port = args.port + try: p = pathoc.Pathoc(args.ssl, args.host, port, None) for i in args.request: - p.request(i) + ret = p.request(i) + if args.verbose: + pathoc.print_full(sys.stdout, *ret) + else: + pathoc.print_short(sys.stdout, *ret) + except pathoc.PathocError, v: - print >> sys.stderr, str(v) + print >> sys.stderr, str(v) sys.exit(1)