2016-09-13 71 views
0

我有一个简单的长轮询事情使用python3requests包。目前,它看起来像:从python3请求生成http错误

def longpoll(): 
    session = requests.Session() 
    while True: 
     try: 
      fetched = session.get(MyURL) 
      input = base64.b64decode(fetched.content) 
      output = process(data) 
      session.put(MyURL, data=base64.b64encode(response)) 
     except Exception as e: 
      print(e) 
      time.sleep(10) 

有一种情况,而不是process荷兰国际集团的输入和put荷兰国际集团的结果的情况下,我想提出一个HTTP错误。是否有一个简单的方法可以从高层次Session界面执行此操作?还是我不得不向下钻取以使用较低级别的对象?

+0

您是否也可以控制远程服务器? –

+0

是的,我也控制了远程服务器。 –

回答

0

既然你已经在服务器控件,你可能要扭转第二个呼叫

下面是一个使用瓶recive第二民调

def longpoll(): 
    session = requests.Session() 
    while True: #I'm guessing that the server does not care that we call him a lot of times ... 
     try: 
      session.post(MyURL, {"ip_address": my_ip_address}) # request work or I'm alive 
      #input = base64.b64decode(fetched.content) 
      #output = process(data) 
      #session.put(MyURL, data=base64.b64encode(response)) 
     except Exception as e: 
      print(e) 
      time.sleep(10) 

@bottle.post("/process") 
def process_new_work(): 
    data = bottle.request.json() 
    output = process(data) #if an error is thrown an HTTP error will be returned by the framework 
    return output 

这样,服务器将得到一个输出实例或错误的HTTP状态