Search code examples
pythonrequestsleeptracker

PYTHON : How to make my request.get() last for a few second?


To test my API, I need to send a request on my viewer url on which there is a tracking service that tell my API how many time I've spent on the page (classical).

I have this small function in my tests :

def does_it_track(response, **kwargs):
  # some unrelated actions
  r = requests.get('my_viewer_url')

This request works fine but it only last for less than a second and it doesn't allow me to test my statistic generator, neither the my tracker precision.

I've tried :

Does someone know about a "not-to-complicated-way" to make my request wait on my page ?

I'm python 2.7

Thank you !


Solution

  • "how many time you've spent on the page" has nothing to do with the HTTP request/response cycle, but with your browser.

    From the server's point of view, the server gets a request, returns a response and the job is over, period - and from the client's point of view once the server returned a response the HTTP transaction is over too. There's not even a notion of "page" here, only HTTP request and response.

    Your "tracker" is (obviously) using javascript to send data from the browser itself (most likely by sending a request each X seconds indicating the page is still displayed in the browser). IOW, the only way to test this is to use a headless browser that will execute javascript.