2016-09-13 70 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狀態