简体   繁体   中英

Getting Python HttpServer response instantly

I am using Python HttpServer in the server side. 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' .

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 ?

You can use python threading

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 . Then you can use stream to reach your requirements.

NOTE: Next code is base on python2 , you can change http server related to python3 related api if you like.

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:

import requests

r = requests.get('http://127.0.0.1:8080', stream=True)
print(r.headers['User-Information'])
print(r.content)

Explain:

  1. 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'])

  2. 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.

Output: (The first line will be shown to user at once, but the second line will be shown 10 seconds later)

Fetching module X. Please wait

Main content here.

Attach the guide for your reference, hope it helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM