screenshare/venv/lib/python3.12/site-packages/easyprocess/examples/with.py
2024-11-29 18:15:30 +00:00

19 lines
552 B
Python

import os
import sys
import urllib.request
from os.path import abspath, dirname
from time import sleep
from easyprocess import EasyProcess
webserver_code = """
from http.server import HTTPServer, CGIHTTPRequestHandler
srv = HTTPServer(server_address=("", 8080), RequestHandlerClass=CGIHTTPRequestHandler)
srv.serve_forever()
"""
os.chdir(dirname(abspath(__file__)))
with EasyProcess([sys.executable, "-c", webserver_code]):
sleep(2) # wait for server
html = urllib.request.urlopen("http://localhost:8080").read().decode("utf-8")
print(html)