1899
1899
use_ipv6: Boolean; to use IPv6 or not
1901
1901
def __init__(self, server_address, RequestHandlerClass,
1902
interface=None, use_ipv6=True):
1902
interface=None, use_ipv6=True, socketfd=None):
1903
"""If socketfd is set, use that file descriptor instead of
1904
creating a new one with socket.socket().
1903
1906
self.interface = interface
1905
1908
self.address_family = socket.AF_INET6
1909
if socketfd is not None:
1910
# Save the file descriptor
1911
self.socketfd = socketfd
1912
# Save the original socket.socket() function
1913
self.socket_socket = socket.socket
1914
# To implement --socket, we monkey patch socket.socket.
1916
# (When socketserver.TCPServer is a new-style class, we
1917
# could make self.socket into a property instead of monkey
1918
# patching socket.socket.)
1920
# Create a one-time-only replacement for socket.socket()
1921
@functools.wraps(socket.socket)
1922
def socket_wrapper(*args, **kwargs):
1923
# Restore original function so subsequent calls are
1925
socket.socket = self.socket_socket
1926
del self.socket_socket
1927
# This time only, return a new socket object from the
1928
# saved file descriptor.
1929
return socket.fromfd(self.socketfd, *args, **kwargs)
1930
# Replace socket.socket() function with wrapper
1931
socket.socket = socket_wrapper
1932
# The socketserver.TCPServer.__init__ will call
1933
# socket.socket(), which might be our replacement,
1934
# socket_wrapper(), if socketfd was set.
1906
1935
socketserver.TCPServer.__init__(self, server_address,
1907
1936
RequestHandlerClass)
1908
1938
def server_bind(self):
1909
1939
"""This overrides the normal server_bind() function
1910
1940
to bind to an interface if one was specified, and also NOT to
1969
1999
def __init__(self, server_address, RequestHandlerClass,
1970
2000
interface=None, use_ipv6=True, clients=None,
1971
gnutls_priority=None, use_dbus=True):
2001
gnutls_priority=None, use_dbus=True, socketfd=None):
1972
2002
self.enabled = False
1973
2003
self.clients = clients
1974
2004
if self.clients is None:
2165
2196
parser.add_argument("--no-restore", action="store_false",
2166
2197
dest="restore", help="Do not restore stored"
2199
parser.add_argument("--socket", type=int,
2200
help="Specify a file descriptor to a network"
2201
" socket to use instead of creating one")
2168
2202
parser.add_argument("--statedir", metavar="DIR",
2169
2203
help="Directory to save/restore state in")
2204
2239
if server_settings["port"]:
2205
2240
server_settings["port"] = server_config.getint("DEFAULT",
2242
if server_settings["socket"]:
2243
server_settings["socket"] = server_config.getint("DEFAULT",
2245
# Later, stdin will, and stdout and stderr might, be dup'ed
2246
# over with an opened os.devnull. But we don't want this to
2247
# happen with a supplied network socket.
2248
if 0 <= server_settings["socket"] <= 2:
2249
server_settings["socket"] = os.dup(server_settings
2207
2251
del server_config
2209
2253
# Override the settings from the config file with command line
2211
2255
for option in ("interface", "address", "port", "debug",
2212
2256
"priority", "servicename", "configdir",
2213
2257
"use_dbus", "use_ipv6", "debuglevel", "restore",
2258
"statedir", "socket"):
2215
2259
value = getattr(options, option)
2216
2260
if value is not None:
2217
2261
server_settings[option] = value