-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlongrun_proc_with_http.py
37 lines (29 loc) · 996 Bytes
/
longrun_proc_with_http.py
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
30
31
32
33
34
35
36
37
import subprocess
import threading
import time
import urllib
def output_reader(proc):
for line in iter(proc.stdout.readline, b''):
print('got line: {0}'.format(line.decode('utf-8')), end='')
def main():
proc = subprocess.Popen(['python3', '-u', '-m', 'http.server', '8070'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
t = threading.Thread(target=output_reader, args=(proc,))
t.start()
try:
time.sleep(0.2)
for i in range(4):
resp = urllib.request.urlopen('http://localhost:8070')
assert b'Directory listing' in resp.read()
time.sleep(0.1)
finally:
proc.terminate()
try:
proc.wait(timeout=0.2)
print('== subprocess exited with rc =', proc.returncode)
except subprocess.TimeoutExpired:
print('subprocess did not terminate in time')
t.join()
if __name__ == '__main__':
main()