aboutsummaryrefslogtreecommitdiff
path: root/debian/tests/single_request_py3
blob: f15dbb563701f9d566c6f9c819a9b230e37949b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python3

from multiprocessing import Process, Pipe
from http.server import HTTPServer, BaseHTTPRequestHandler
import requests

class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", 'text/plain')
        self.end_headers()
        self.wfile.write(b'test')

    def log_request(self, code='-', size='-'):
        pass

def start_server(pipe):
    # start a simple server on any available port
    httpd = HTTPServer(('', 0), RequestHandler)
    pipe.send(httpd.socket.getsockname())
    httpd.handle_request()

if __name__ == '__main__':
    parent, child = Pipe()
    p = Process(target=start_server, args=(child,))
    p.daemon = True
    p.start()
    # use the socket address from the test server
    requests.get('http://{}:{}'.format(*parent.recv()))