PageRenderTime 14ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/silk/sock_http.py

https://bitbucket.org/btubbs/silk-deployment/
Python | 41 lines | 35 code | 4 blank | 2 comment | 0 complexity | f297e1ac677cd74b72c777212d6abdd8 MD5 | raw file
 1#!/usr/bin/env python
 2
 3import sys
 4import socket
 5
 6REQUEST_TEMPLATE = ('%(method)s %(path)s HTTP/1.1\r\n'
 7                    'Host: %(host)s\r\n\r\n')
 8
 9SUPPORTED_METHODS = ('HEAD', 'GET')
10
11def sockhttp(sockpath, method, path, host):
12    """Make an HTTP request over a unix socket."""
13    req = REQUEST_TEMPLATE % locals()
14    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
15    s.connect(sockpath)
16    s.send(req)
17    out = ''
18    while 1:
19        data = s.recv(1024)
20        out += data
21        if not data: break
22    s.close()
23    return out
24
25def usage():
26    """Print usage information for this program"""
27    print ("This program allows you to make http requests to unix sockets. "
28           "Usage:\n\n"
29           "python %s /path/to/socket METHOD request_path host_name\n" %
30           __file__)
31
32    print "Supported methods are: %s" % ", ".join(SUPPORTED_METHODS)
33
34if __name__ == '__main__':
35    try:
36        _, sockpath, method, path, host = sys.argv
37    except ValueError:
38        usage()
39        sys.exit(1)
40    print sockhttp(sockpath, method, path, host)
41