2012-06-29 22:51:13 +00:00
|
|
|
import sys
|
2012-06-24 09:10:10 +00:00
|
|
|
from netlib import tcp, http
|
|
|
|
import rparse
|
|
|
|
|
|
|
|
class PathocError(Exception): pass
|
|
|
|
|
|
|
|
|
2012-06-24 10:59:20 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2012-06-24 09:10:10 +00:00
|
|
|
class Pathoc(tcp.TCPClient):
|
2012-06-25 22:15:11 +00:00
|
|
|
def __init__(self, host, port):
|
2012-06-29 22:51:13 +00:00
|
|
|
tcp.TCPClient.__init__(self, host, port)
|
2012-06-24 09:10:10 +00:00
|
|
|
|
|
|
|
def request(self, spec):
|
2012-06-26 05:28:07 +00:00
|
|
|
"""
|
|
|
|
Return an (httpversion, code, msg, headers, content) tuple.
|
2012-06-29 22:51:13 +00:00
|
|
|
|
|
|
|
May raise rparse.ParseException and netlib.http.HttpError.
|
2012-06-26 05:28:07 +00:00
|
|
|
"""
|
2012-06-24 09:10:10 +00:00
|
|
|
r = rparse.parse_request({}, spec)
|
|
|
|
r.serve(self.wfile)
|
|
|
|
self.wfile.flush()
|
2012-06-24 10:59:20 +00:00
|
|
|
return http.read_response(self.rfile, r.method, None)
|
2012-06-29 22:51:13 +00:00
|
|
|
|
|
|
|
def print_requests(self, reqs, verbose, fp=sys.stdout):
|
|
|
|
"""
|
|
|
|
Performs a series of requests, and prints results to the specified
|
|
|
|
file pointer.
|
|
|
|
"""
|
|
|
|
for i in reqs:
|
|
|
|
try:
|
|
|
|
ret = self.request(i)
|
|
|
|
except rparse.ParseException, v:
|
|
|
|
print >> fp, "Error parsing request spec: %s"%v.msg
|
|
|
|
print >> fp, v.marked()
|
|
|
|
return
|
|
|
|
except http.HttpError, v:
|
|
|
|
print >> fp, v.msg
|
|
|
|
return
|
|
|
|
if verbose:
|
|
|
|
print_full(fp, *ret)
|
|
|
|
else:
|
|
|
|
print_short(fp, *ret)
|