aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod/pathoc.py
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2012-06-30 10:51:13 +1200
committerAldo Cortesi <aldo@nullcube.com>2012-06-30 10:51:13 +1200
commit2cb55ee0f5b00c2c3f4f6d9ba9360c31b82b095c (patch)
tree585965edd2745d565251003877eee8e845afdcce /libpathod/pathoc.py
parent654a84174adbb323423d4a5a0a9c3945df073610 (diff)
downloadmitmproxy-2cb55ee0f5b00c2c3f4f6d9ba9360c31b82b095c.tar.gz
mitmproxy-2cb55ee0f5b00c2c3f4f6d9ba9360c31b82b095c.tar.bz2
mitmproxy-2cb55ee0f5b00c2c3f4f6d9ba9360c31b82b095c.zip
Factor out request printing in to a method, and test it.
Diffstat (limited to 'libpathod/pathoc.py')
-rw-r--r--libpathod/pathoc.py28
1 files changed, 24 insertions, 4 deletions
diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py
index 9320b1c5..3791c116 100644
--- a/libpathod/pathoc.py
+++ b/libpathod/pathoc.py
@@ -1,3 +1,4 @@
+import sys
from netlib import tcp, http
import rparse
@@ -16,16 +17,35 @@ def print_full(fp, httpversion, code, msg, headers, content):
class Pathoc(tcp.TCPClient):
def __init__(self, host, port):
- try:
- tcp.TCPClient.__init__(self, host, port)
- except tcp.NetLibError, v:
- raise PathocError(v)
+ tcp.TCPClient.__init__(self, host, port)
def request(self, spec):
"""
Return an (httpversion, code, msg, headers, content) tuple.
+
+ May raise rparse.ParseException and netlib.http.HttpError.
"""
r = rparse.parse_request({}, spec)
r.serve(self.wfile)
self.wfile.flush()
return http.read_response(self.rfile, r.method, None)
+
+ 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)