使用';而对';在Python 2.7中执行http请求时

使用';而对';在Python 2.7中执行http请求时,python,python-2.7,python-requests,Python,Python 2.7,Python Requests,是否有一种更符合python(2.7)的方法来检查服务器的良好状态代码(200),其中不包括使用,而使用True?我的代码片段如下所示——它被多次调用: import time import json from datetime import datetime import requests while True: response = requests.get('http://example.com') if respo

是否有一种更符合python(2.7)的方法来检查服务器的良好状态代码(200),其中不包括使用
,而使用True
?我的代码片段如下所示——它被多次调用:

    import time
    import json
    from datetime import datetime
    import requests

    while True:
        response = requests.get('http://example.com')
        if response.status_code != 200:
            print 'sleeping:',str(datetime.now()),response.status_code
            print 'sleeping:',str(datetime.now()),response.headers
            time.sleep(5.0)
        else: break
    if "x-mashery-error-code" in response.headers:
        return None
    return response.json()
编辑:我包含了带有标题错误的“if”循环。

您可以使用


我希望此解决方案:

response = requests.get('http://example.com')
while response.status_code != 200:
    print 'sleeping:',str(datetime.now()),response.status_code
    print 'sleeping:',str(datetime.now()),response.headers
    time.sleep(5.0)
    response = requests.get('http://example.com')
因为:

>>> import this
...
Explicit is better than implicit.
Simple is better than complex.
...
Flat is better than nested.
...
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
...
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
...

因为我一读就明白了。对于事件挂钩,情况并非如此。他们是否打开线程来并行检索字节?什么时候叫来?我需要自己检索数据吗?

我正在使用面向方面的编程,通过应用装饰器来进行类似于重试的操作。如果用于获取所需值的函数如下所示:

def getValue():
  return requests.get('http://example.com')
然后,我对该函数进行了修饰,以应用重试机制,而不干扰原始(原始)代码:

以上是准备工作(实用程序库的一部分),以下是用法:

@retryUntilCondition(responseIs200)
def getValue():
  return requests.get('http://example.com')
通过这种方式,
while
循环对应用程序代码完全隐藏,并且不会使读取复杂化。重试的方面是通过预先添加一个简单的装饰器来添加的,该装饰器甚至可以在其他情况下重用


如果以后您决定只想重试特定次数,有不同的延迟等,所有这些都可以在
重试
装饰器中单独实现。

此代码用于检查服务器的运行状况吗?@praveen否,我需要响应中的数据。json()。但是服务器有一个限制,并且可能发送非200个代码。如果问题出在
上,则使用
而1:
这是显而易见的解决方案。但真正的问题是什么?你能写更多关于这方面的内容吗?“while True”不是问题,但我想问的是,现在pythonal中是否有更好的实践方法。那么看看这个:有一个重试计数器,它可能会在throttling的情况下阻碍你,非常好。睡眠声明也可能包含在“如果”循环中?我不认为你真的需要睡眠。。。您可以通过这种方式异步发送请求。如果您想等待请求完成,您只需在下一个url达到状态200时为其调用另一个方法即可。你应该试着换一种想法;)我喜欢它,但这不是使服务器的请求数加倍吗?这种解决方案的主要缺点是代码加倍。如果URL后来更改,很有可能您忘记在两个位置对其进行调整(考虑到这两个位置的距离更远)。@philshem它与您的代码完全一样。@阿尔夫,我想你可以把它重构成一个变量。或者您可以将整个请求重构为一个函数。你能做到。@user啊,是的,我看到了。我接受(并使用)了这个解决方案,因为它允许非核心程序员将来修改。同样出于我的目的,添加一个计数器在N次尝试后退出程序是很容易的。这是一个非常优雅的解决方案,但我需要一些其他人以后可以修改的东西。
def retryUntilCondition(condition):
  def decorate(function):
    def f(*args, **kwargs):
      while True:
        result = function(*args, **kwargs)
        if condition(result):
          return result
        time.sleep(5.0)
    return f
  return decorate

def responseIs200(response):
  return response.status_code == 200
@retryUntilCondition(responseIs200)
def getValue():
  return requests.get('http://example.com')