Any interest in an UnixHTTPConnectionPool?
See original GitHub issueI’m happy to create a PR, but it seems like a pretty common use case to want to establish an HTTP connection over a unix socket.
I see this abstraction happening more commonly through requests, but was a little surprised that this doesn’t just exist as a connection within urllib3.
The code I’ve thrown together (not yet battle tested) is pretty trivial to do when you get rid of the requests bits, and it’s boiled down to just:
class UnixHTTPConnection(HTTPConnection):
def __init__(self, host, **kwargs):
# We're using the `host` as the socket path, but
# urllib3 uses this host as the Host header by default.
# If we send along the socket path as a Host header, this is
# never what you want and would typically be malformed value.
# So we fake this by sending along `localhost` by default as
# other libraries do.
self.socket_path = host
super(UnixHTTPConnection, self).__init__(host='localhost', **kwargs)
def _new_conn(self):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
if self.timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
sock.settimeout(self.timeout)
sock.connect(self.socket_path)
return sock
class UnixHTTPConnectionPool(HTTPConnectionPool):
ConnectionCls = UnixHTTPConnection
If this is something you’d be interested in pulling into urllib3 as a core connection pool, I think this would be great to have, and I’ll convert this into a PR. If not, would be curious if this seems outside the scope of urllib3 or some other reason.
The sources of this off the top of my head are within docker-py, which again, uses this sorta abstraction except a bit heavy handed since it also needs the requests adapter https://github.com/docker/docker-py/blob/master/docker/transport/unixconn.py. Then requests-unixsocket project https://github.com/msabramo/requests-unixsocket.
And again, nothing explicitly for those of us that like just directly using urllib3.
I plan on adding this into Sentry very soon, and will cut over to using this for some of our critical internal services to get it battle tested.
I also noticed that this is sorta being used within test suite: https://github.com/urllib3/urllib3/blob/0f85e05af9ef2ded671a7b47506dfd24b32decf0/test/test_connectionpool.py#L35-L45 I’m not entirely sure of the context here, but it’s definitely just used for some mock class. Seems relevant though. 😃
Issue Analytics
- State:
- Created 5 years ago
- Comments:21 (21 by maintainers)

Top Related StackOverflow Question
since requests is our primary downstream user, let’s match their scheme. It’s also a scheme that makes sense, nginx’s just looks… weird.
I’m fine with UnixHTTPConnection being in contrib.
Yeah. I think we just want to be explicit in the documentation (like a
.. warningat the top) that this does not work with PoolManager. That should help deflect most issues.