本文整理汇总了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.")