本文整理汇总了Python中socket.socket.getsockname方法的典型用法代码示例。如果您正苦于以下问题:Python socket.getsockname方法的具体用法?Python socket.getsockname怎么用?Python socket.getsockname使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket.socket
的用法示例。
在下文中一共展示了socket.getsockname方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Bus
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import getsockname [as 别名]
class Bus(common.AutoClose):
"""
An Autobus bus. Busses manage a set of published services, and allow
connecting to other services. A single bus listens on a single TCP
port and multiplexes all published services over it.
Bus is a subclass of ServiceProvider; the service it provides is a service
exposing information about what other services, events, functions, and
objects are present. (This service is more commonly known as the
introspection service.) You normally won't have to know this; instances of
Bus register themselves as services with themselves, so you don't need to
do anything to make the introspection service work.
"""
def __init__(self, default_discoverers=True, default_publishers=True,
port=None):
"""
Creates a new bus. The bus will listen on the specified port; if none
is specified (which is the usual case), a port will be chosen from the
ports not currently in use on this computer.
If default_discoverers is True (the default), a default set of
discoverers will be installed, and likewise for default_publishers.
Right now, this simply installs a autobus2.discovery.BroadcastPublisher
and autobus2.discovery.BroadcastDiscoverer. Others might be added in
the future.
"""
# Number of times this bus has been __enter__'d. Allows it to be used
# as a re-entrant context manager.
self.context_enters = 0
if port is None:
port = 0
# True once close() has been called
self.closed = False
# The TCP server that will listen for connections
self.server = Socket()
self.server.bind(("", port))
# TODO: make the backlog configurable
self.server.listen(100)
self.port = self.server.getsockname()[1]
# Lock that nearly everything bus-related locks on
self.lock = RLock()
# PropertyTable whose keys are service ids and whose values are
# instances of autobus2.local.LocalService
self.local_services = PropertyTable()
self.local_services.global_watch(self.local_service_changed)
# Map of ids of discovered services to DiscoveredService instances
self.discovered_services = {}
self.discovery_listeners = []
# List of (filter, function) tuples, where filter is an info object
# filter and function is a function to be notified when a matching
# service is created or deleted
self.service_listeners = []
# Set of RemoteConnection instances that have been bound to a service
self.bound_connections = set()
# Set of discoverers registered on this bus
self.discoverers = set()
# Set of publishers registered on this bus
self.publishers = set()
if default_discoverers:
self.install_discoverer(discovery.BroadcastDiscoverer())
if default_publishers:
self.install_publisher(discovery.BroadcastPublisher())
Thread(name="autobus2.Bus.accept_loop", target=self.accept_loop).start()
# Disable the introspection service for now. I'm seeing what would
# happen if I have per-service introspection functions and objects, so
# I'm disabling the bus-wide introspection service.
# self._create_introspection_service()
#
# Register the bus as a service on itself.
self.create_service({"type": "autobus.details", "pid": os.getpid()}, _IntrospectionService(self))
def accept_loop(self):
"""
Called on a new thread to accept socket connections to this bus.
"""
self.server.settimeout(1)
while not self.closed:
try:
socket = None
socket = self.server.accept()[0]
self.setup_inbound_socket(socket)
except SocketTimeout: # This happens when we time out, which is
# normal. The 1-second timeout is to fix what appears to be a
# bug with Windows not properly throwing an exception from
# accept when another thread closes the socket.
pass
except: # This happens when the server socket is closed
if socket:
socket.close() # Make sure it's /really/ closed on the
# off chance that something else caused the exception
if not issubclass(sys.exc_type, SocketError): # Something else
# happened
print_exc()
# print "Bus server died"
return
@synchronized_on("lock")
def create_service(self, info, provider):
"""
Creates a new service on this bus. info is the info object to use for
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import getsockname [as 别名]
def __init__(self, runner: 'BaseRunner', sock: socket.socket, *,
shutdown_timeout: float=60.0,
ssl_context: Optional[SSLContext]=None,
backlog: int=128) -> None:
super().__init__(runner, shutdown_timeout=shutdown_timeout,
ssl_context=ssl_context, backlog=backlog)
self._sock = sock
scheme = 'https' if self._ssl_context else 'http'
if hasattr(socket, 'AF_UNIX') and sock.family == socket.AF_UNIX:
name = '{}://unix:{}:'.format(scheme, sock.getsockname())
else:
host, port = sock.getsockname()[:2]
name = str(URL.build(scheme=scheme, host=host, port=port))
self._name = name
示例3: __init__
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import getsockname [as 别名]
def __init__(self):
# Set up the interrupt socket
interrupt_server = Socket()
interrupt_server.bind(("localhost", 0))
interrupt_server.listen(1)
self.interrupt_writer = Socket()
self.interrupt_writer.setblocking(False)
self.interrupt_writer.connect("localhost", interrupt_server.getsockname()[1])
self.interrupt_reader = interrupt_server.accept()
interrupt_server.shutdown(SHUT_RDWR)
interrupt_server.close()
self.interrupt_reader.setblocking(False)
self.interrupt_writer.setblocking(False)
示例4: match_socket
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import getsockname [as 别名]
def match_socket(self, sock: socket.socket, address: IPv6Address, interface: int = 0) -> bool:
"""
Determine if we can recycle this socket
:param sock: An existing socket
:param address: The address we want
:param interface: The interface number we want
:return: Whether the socket is suitable
"""
if sock.family != socket.AF_INET6 or sock.type != self.sock_type or sock.proto != self.sock_proto:
# Different protocol
return False
sockname = sock.getsockname()
if IPv6Address(sockname[0].split('%')[0]) != address \
or sockname[1] != self.listen_port \
or sockname[3] != interface:
# Wrong address
return False
# Amazing! This one seems to match
return True
示例5: tcp_connect_handler
# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import getsockname [as 别名]
def tcp_connect_handler(sock:socket.socket, remote:list, server:socketserver.TCPServer):
now = time.strftime("%Y-%m-%d %H:%M:%S")
print("[%s] Incoming connection on %s:%s from %s:%s" % (now,sock.getsockname()[0],sock.getsockname()[1],remote[0],remote[1]))
#sock.shutdown(socket.SHUT_RDWR)
sock.close()