silk-deployment / silk / sock_http.py

#!/usr/bin/env python

import sys
import socket

REQUEST_TEMPLATE = ('%(method)s %(path)s HTTP/1.1\r\n'
                    'Host: %(host)s\r\n\r\n')

SUPPORTED_METHODS = ('HEAD', 'GET')

def sockhttp(sockpath, method, path, host):
    """Make an HTTP request over a unix socket."""
    req = REQUEST_TEMPLATE % locals()
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    s.connect(sockpath)
    s.send(req)
    out = ''
    while 1:
        data = s.recv(1024)
        out += data
        if not data: break
    s.close()
    return out

def usage():
    """Print usage information for this program"""
    print ("This program allows you to make http requests to unix sockets. "
           "Usage:\n\n"
           "python %s /path/to/socket METHOD request_path host_name\n" %
           __file__)

    print "Supported methods are: %s" % ", ".join(SUPPORTED_METHODS)

if __name__ == '__main__':
    try:
        _, sockpath, method, path, host = sys.argv
    except ValueError:
        usage()
        sys.exit(1)
    print sockhttp(sockpath, method, path, host)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.