Skip to content

Commit 27b42be

Browse files
committed
Better support for paths in Unix server mode
1 parent 428071a commit 27b42be

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

jsonrpclib/jsonrpc.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
# Standard library
6161
import contextlib
6262
import logging
63+
import os
6364
import socket
6465
import sys
6566
import uuid
@@ -497,9 +498,15 @@ class UnixTransport(TransportMixIn, XMLTransport):
497498
"""
498499
Mixed-in HTTP transport over a UNIX socket
499500
"""
500-
def __init__(self, config):
501+
def __init__(self, config, path=None):
502+
"""
503+
:param config: The jsonrpclib configuration
504+
:param path: Path to the Unix socket (overrides the host name later)
505+
"""
501506
TransportMixIn.__init__(self, config)
502507
XMLTransport.__init__(self)
508+
# Keep track of the given path, if any
509+
self.__unix_path = os.path.abspath(path) if path else None
503510

504511
def make_connection(self, host):
505512
"""
@@ -510,11 +517,15 @@ def make_connection(self, host):
510517
511518
Code copied from xmlrpc.client (Python 3)
512519
513-
:param host: Target host
520+
:param host: Target host (ignored if a path was given)
514521
:return A UnixHTTPConnection object
515522
"""
523+
if self.__unix_path:
524+
host = self.__unix_path
525+
516526
if self._connection and host == self._connection[0]:
517527
return self._connection[1]
528+
518529
# create a HTTP connection object from a host descriptor
519530
path, self._extra_headers, _ = self.get_host_info(host)
520531
self._connection = host, UnixHTTPConnection(path)
@@ -560,14 +571,21 @@ def __init__(self, uri, transport=None, encoding=None,
560571
raise IOError('Unsupported JSON-RPC protocol.')
561572

562573
self.__host, self.__handler = splithost(uri)
563-
if not self.__handler:
574+
if use_unix:
575+
unix_path = self.__handler
576+
self.__handler = '/'
577+
elif not self.__handler:
564578
# Not sure if this is in the JSON spec?
565579
self.__handler = '/'
566580

567581
if transport is None:
568582
if use_unix:
569583
if schema == "http":
570-
transport = UnixTransport(config=config)
584+
# In Unix mode, we use the path part of the URL (handler)
585+
# as the path to the socket file
586+
transport = UnixTransport(
587+
config=config, path=unix_path
588+
)
571589
elif schema == 'https':
572590
transport = SafeTransport(config=config, context=context)
573591
else:

0 commit comments

Comments
 (0)