Python等待100秒并返回True或False

时间:2018-09-21 11:25:27

标签: python python-2.7 function while-loop

我下面有python脚本,用于检查ping是否成功,如果在100秒内成功,它将返回True。如果ping失败,则应返回False,但不会返回False,当ping成功时,它将返回True。

任何人都可以在下面的代码中修复为什么它不返回False

代码:

@Service
public class ImplementaionClass implements Service {
    private final RestTemplate restTemplate;
    public RecipeServiceImpl(RestTemplate restTemplate) {
        // TODO Auto-generated constructor stub
        this.restTemplate = restTemplate;
    }
    @Override
    public List<Result> getResultsByRecipe(String search,String query,int pageno) {
        // TODO Auto-generated method stub
        final List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
        messageConverters.add(converter);

//      
        this.restTemplate.setMessageConverters(messageConverters);
//
//      final Recipe recipe = this.restTemplate.getForObject("http://url.../api/?i=" + search,
//              Recipe.class);
//      
        ResponseEntity<Recipe> rateResponse = null;
//      ResponseEntity<Recipe> rateResponse = null;
//      if(pageno==0)
//          rateResponse =     restTemplate.exchange("http://url.../api/?i="+search,
//
//                    HttpMethod.GET, null, new ParameterizedTypeReference<Recipe>() {
//            });
        if(query.isEmpty())
            if(pageno==0)enter code here
                rateResponse =     restTemplate.exchange("http://url..../api/?i=" + search,
                        HttpMethod.GET, null, new ParameterizedTypeReference<Recipe>() {
                });
            else
            rateResponse =     restTemplate.exchange("http://url..../api/?i=" + search+"&q=&p="+pageno,
                                HttpMethod.GET, null, new ParameterizedTypeReference<Recipe>() {
                        });
        else
            if(pageno==0)
                rateResponse =     restTemplate.exchange("http://url.../api/?i=" + search+"&q="+query,
                        HttpMethod.GET, null, new ParameterizedTypeReference<Recipe>() {
                });
            else 
                rateResponse =     restTemplate.exchange("http://url...../api/?i=" + search+"&q="+query+"&p="+pageno,
                    HttpMethod.GET, null, new ParameterizedTypeReference<Recipe>() {
            });
final Recipe rates = rateResponse.getBody();

        System.out.println(rateResponse.getHeaders());
    return rates.getResults();
    }

1 个答案:

答案 0 :(得分:-1)

以下代码将为您工作:

import time

def ping(self, hostname, try_for=100):
    t_end = time.time() + try_for
    is_succeed = False
    while time.time() < t_end or is_succeed:
        time.sleep(1)
        data = self.pingCheck("ping 10.10.10.1 count 5")
        is_succeed = "Success" in data
    return is_succeed

我已经将等待时间定义为名为try_for的参数,该参数的默认值设置为100,但是您可以通过任何其他等待主机可用性的秒数。