docker容器使用不了ping_为什么我不能在python中ping docker容器?

您的问题是您运行容器,而不是等待服务出现。你应该等服务生起来。你可以用下面这样的东西def wait_net_service(server, port, timeout=None):

""" Wait for network service to appear

@param timeout: in seconds, if None or 0 wait forever

@return: True of False, if timeout is None may return only True or

throw unhandled network exception

"""

import socket

import errno

s = socket.socket()

if timeout:

from time import time as now

# time module is needed to calc timeout shared between two exceptions

end = now() + timeout

while True:

try:

if timeout:

next_timeout = end - now()

if next_timeout < 0:

return False

else:

s.settimeout(next_timeout)

s.connect((server, port))

except socket.timeout, err:

# this exception occurs only if timeout is set

if timeout:

return False

except socket.error, err:

# catch timeout exception from underlying network library

# this one is different from socket.timeout

if type(err.args) != tuple or err[0] != errno.ETIMEDOUT:

raise

else:

s.close()

return True

然后将代码更新为

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值