aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod/pathoc.py
blob: 9320b1c5e881a0be9e8e86e766bd7abd466d6f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from netlib import tcp, http
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, host, port):
        try:
            tcp.TCPClient.__init__(self, host, port)
        except tcp.NetLibError, v:
            raise PathocError(v)

    def request(self, spec):
        """
            Return an (httpversion, code, msg, headers, content) tuple.
        """
        r = rparse.parse_request({}, spec)
        r.serve(self.wfile)
        self.wfile.flush()
        return http.read_response(self.rfile, r.method, None)