[英]Getting Python HttpServer response instantly
I am using Python HttpServer
in the server side. 我在服务器端使用Python HttpServer
。 One GET
request will take more time to respond and I want to update the user the current status of it, such as 'Fetching module X. Please wait'
, 'Fetching module Y. Please wait'
. 一个GET
请求将花费更多时间来响应,并且我想更新用户的当前状态,例如'Fetching module X. Please wait'
, 'Fetching module Y. Please wait'
。
But, it is not getting updated in the client side even though I sending it in between the modules. 但是,即使我在模块之间发送它,也不会在客户端进行更新。 I have tried flushing the stream, but no luck. 我曾尝试冲洗河流,但没有运气。
self.wfile.write('Fetching module X. Please wait')
self.wfile.flush()
How can I force the HttpServer
to send the information instantly, instead of waiting for the completion of full response ? 如何强制HttpServer
立即发送信息,而不是等待完整响应的完成?
You can use python threading 您可以使用python线程
from threading import Thread
t = threading.Thread(target=function to be call, args=[request])
t.setDaemon(False)
t.start()
This code will force to return response instantly and run your function in background. 此代码将强制立即返回响应并在后台运行您的函数。
Suggest you put the user indication
to header
not body
. 建议您将user indication
放在header
而非body
。 Then you can use stream
to reach your requirements. 然后,您可以使用stream
来满足您的要求。
NOTE: Next code is base on python2
, you can change http server related
to python3 related api
if you like. 注意:下一个代码基于python2
,您可以根据需要更改http server related
python3 related api
http server related
。
server.py: server.py:
import BaseHTTPServer
import time
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
Page = "Main content here."
def do_GET(self):
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.send_header("Content-Length", str(len(self.Page)))
self.send_header("User-Information", "Fetching module X. Please wait")
self.end_headers()
time.sleep(10)
self.wfile.write(self.Page)
if __name__ == '__main__':
serverAddress = ('', 8080)
server = BaseHTTPServer.HTTPServer(serverAddress, RequestHandler)
server.serve_forever()
client.py: client.py:
import requests
r = requests.get('http://127.0.0.1:8080', stream=True)
print(r.headers['User-Information'])
print(r.content)
Explain: 说明:
If use stream
, the header information
will still be fetched by client at once, so you can print it to user at once with print(r.headers['User-Information'])
如果使用stream
,则header information
仍将由客户端立即获取,因此您可以使用print(r.headers['User-Information'])
一次将其print(r.headers['User-Information'])
给用户。
But with stream
, the body information
will not transmit, it's be delayed until client use r.content
to require it( Response.iter_lines() or Response.iter_content()
also ok), so when you do print(r.content)
, it will need 10 seconds
to see the main content as it cost 10s
in server code. 但是使用stream
, body information
将不会传输,它会延迟到客户端使用r.content
要求它时( Response.iter_lines() or Response.iter_content()
也可以),因此当您执行print(r.content)
,它需要10 seconds
看到的主要内容,因为它花费10s
的服务器代码。
Output: (The first line will be shown to user at once, but the second line will be shown 10 seconds later) 输出:(第一行将立即显示给用户,但第二行将在10秒后显示给用户)
Fetching module X. Please wait 正在获取模块X。请稍候
Main content here. 主要内容在这里。
Attach the guide for your reference, hope it helpful. 附上指南以供参考,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.