3

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 ?

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But, in my case, the client side is from browser. :( In that case, how can we get it ?
If direct call from html, I'm afraid not. If call from javascript, maybe websocket?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.