inside Habbie's mind

HTTP on Unix sockets with Python

written by peter, on Aug 15, 2007 2:55:00 PM.

Initially I had a more elaborate version based on the exact connect() code in the higher class, but this simpler version works just fine. Incidentally, what the xen xm commandline tool uses works identically ;)

class UHTTPConnection(httplib.HTTPConnection):
    """Subclass of Python library HTTPConnection that
       uses a unix-domain socket.
    """
 
    def __init__(self, path):
        httplib.HTTPConnection.__init__(self, 'localhost')
        self.path = path
 
    def connect(self):
        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        sock.connect(self.path)
        self.sock = sock

This small hack plus the fine PHP serialization classes from Scott Hurring are the basis of OpenPanel.coreclient, which allows for very easy provisioning and querying of OpenPanel/opencore data. We use it mostly for unit testing.

Comments

  • Do you know if with your code it’s possible in some way to make urllib2.urlopen() work with unix sockets ? I’ve tried with no luck : http://pastebin.ca/694609 Do you see what’s wrong ? TIA

    Comment by tamere — Sep 13, 2007 8:34:17 PM | # - re

Leave a Reply