本文整理匯總了Python中zmq.EADDRINUSE屬性的典型用法代碼示例。如果您正苦於以下問題:Python zmq.EADDRINUSE屬性的具體用法?Python zmq.EADDRINUSE怎麽用?Python zmq.EADDRINUSE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類zmq
的用法示例。
在下文中一共展示了zmq.EADDRINUSE屬性的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: bind_to_random_port
# 需要導入模塊: import zmq [as 別名]
# 或者: from zmq import EADDRINUSE [as 別名]
def bind_to_random_port(self, addr, min_port=49152, max_port=65536, max_tries=100):
"""bind this socket to a random port in a range
Parameters
----------
addr : str
The address string without the port to pass to ``Socket.bind()``.
min_port : int, optional
The minimum port in the range of ports to try (inclusive).
max_port : int, optional
The maximum port in the range of ports to try (exclusive).
max_tries : int, optional
The maximum number of bind attempts to make.
Returns
-------
port : int
The port the socket was bound to.
Raises
------
ZMQBindError
if `max_tries` reached before successful bind
"""
for i in range(max_tries):
try:
port = random.randrange(min_port, max_port)
self.bind('%s:%s' % (addr, port))
except ZMQError as exception:
if not exception.errno == zmq.EADDRINUSE:
raise
else:
return port
raise ZMQBindError("Could not bind socket to random port.")
示例2: runServer
# 需要導入模塊: import zmq [as 別名]
# 或者: from zmq import EADDRINUSE [as 別名]
def runServer(self, endpoint):
self.s = zerorpc.Server(self)
#self.s.bind(endpoint)
try:
self.s.bind(endpoint)
except zmq.ZMQError as e:
if e.errno == zmq.EADDRINUSE:
logging.info("Pyscard RPC server already running\n")
else:
logging.warning("Pyscard RPC server " + endpoint + " could not be started")
self.s.close()
sys.exit(0)
#create result file only when server is started
dir = os.path.dirname(__file__)
resultFile = dir + "/../pyscard_rpc.log"
FORMATTER = logging.Formatter(fmt='%(asctime)s %(message)s', datefmt='%H:%M:%S')
fileHndl = logging.FileHandler(resultFile, mode='w')
fileHndl.setFormatter(FORMATTER)
fileHndl.setLevel(logging.DEBUG)
logger = logging.getLogger()
logger.addHandler(fileHndl)
logging.info("Pyscard RPC Server " + endpoint + " started\n")
self.s.run()
return self
示例3: bind_to_random_port
# 需要導入模塊: import zmq [as 別名]
# 或者: from zmq import EADDRINUSE [as 別名]
def bind_to_random_port(self, addr, min_port=49152, max_port=65536, max_tries=100):
"""Bind this socket to a random port in a range.
If the port range is unspecified, the system will choose the port.
Parameters
----------
addr : str
The address string without the port to pass to ``Socket.bind()``.
min_port : int, optional
The minimum port in the range of ports to try (inclusive).
max_port : int, optional
The maximum port in the range of ports to try (exclusive).
max_tries : int, optional
The maximum number of bind attempts to make.
Returns
-------
port : int
The port the socket was bound to.
Raises
------
ZMQBindError
if `max_tries` reached before successful bind
"""
if hasattr(constants, 'LAST_ENDPOINT') and min_port == 49152 and max_port == 65536:
# if LAST_ENDPOINT is supported, and min_port / max_port weren't specified,
# we can bind to port 0 and let the OS do the work
self.bind("%s:*" % addr)
url = self.last_endpoint.decode('ascii', 'replace')
_, port_s = url.rsplit(':', 1)
return int(port_s)
for i in range(max_tries):
try:
port = random.randrange(min_port, max_port)
self.bind('%s:%s' % (addr, port))
except ZMQError as exception:
en = exception.errno
if en == zmq.EADDRINUSE:
continue
elif sys.platform == 'win32' and en == errno.EACCES:
continue
else:
raise
else:
return port
raise ZMQBindError("Could not bind socket to random port.")