source: test/services/delayed-echo.py @ 3

Last change on this file since 3 was 3, checked in by tim, 9 years ago

.

  • Property svn:executable set to *
File size: 1.1 KB
Line 
1#!/usr/bin/env python3
2#
3# Copyright (C) 2014 Blindspot Security LLC
4# by Timothy D. Morgan
5# twits: @ecbftw
6
7import sys
8import time
9import socketserver
10import http.server
11
12
13class EchoHandler(http.server.BaseHTTPRequestHandler):
14    """
15    """
16
17    def do_GET(self):
18        #resolution = time.clock_getres(time.CLOCK_MONOTONIC)
19        received = int(time.monotonic()*1000000000)
20        wait_time = 0
21        if 't=' in self.path:
22            wait_time = int(self.path.split('t=', 1)[1], 10)
23
24        # Use a busy-wait with monotonic clock.  More accurate than time.sleep()
25        finish = received + wait_time
26        now = int(time.monotonic()*1000000000)
27        while now < finish:
28            now = int(time.monotonic()*1000000000)
29       
30        self.send_response(200)
31        self.send_header('Content-Type','text/plain; charset=UTF-8')
32        self.end_headers()
33
34        content = "waited: %d\n" % (now - received)
35        self.wfile.write(content.encode('utf-8'))
36        self.wfile.flush()
37
38
39if __name__ == "__main__":
40    HOST, PORT = "0.0.0.0", 3240
41    server = socketserver.TCPServer((HOST, PORT), EchoHandler)
42    server.serve_forever()
Note: See TracBrowser for help on using the repository browser.