我使用python3
和requests
包进行了简单的长轮询。它目前看起来像:
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)
有一种情况,我想提出一个http错误,而不是process
输入和put
结果。有没有一种简单的方法可以从高级Session
界面执行此操作?或者我是否需要向下钻取以使用较低级别的对象?
答案 0 :(得分:0)
由于您可以控制服务器,因此您可能想要撤消第二次呼叫
这是一个使用瓶子来回顾第二轮询的例子
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状态