Use http.read_response to read server response.

Also add a few different output formats, and a verbosity flag.
This commit is contained in:
Aldo Cortesi 2012-06-24 22:59:20 +12:00
parent ddda57179a
commit a747043cc8
2 changed files with 22 additions and 7 deletions

View File

@ -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)

15
pathoc
View File

@ -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)