Instant fastcgi scripts by just adding a decorator
from fastcore.utils import *
from fastcore.script import *
import subprocess,time,urllib

class ForkingUnixServer[source]

ForkingUnixServer(server_address, RequestHandlerClass, bind_and_activate=True) :: ForkingMixIn

Mix-in class to handle each request in a new process.

fastcgi[source]

fastcgi(sock='fcgi.sock', func=None)

def c():
    return "Content-type: text/html\r\n\r\n<html>" \
        + f'{sys.stdin.read()} ; {os.environ["QUERY_STRING"]}</html>\r\n'
p = Path('fcgi.sock')

@fastcgi(p)
def hello(): sys.stdout.write(c())

@threaded
def _f():
    with hello() as srv: srv.handle_request()
proc = subprocess.Popen(['./http2fcgi'])
if p.exists(): p.unlink()
t = _f()
time.sleep(0.2)
res = urlread('http://localhost:6065/setup.py?a=1', foo='bar', greeting='你好')
print(urllib.parse.unquote(res.decode()))
proc.terminate()
<html>foo=bar&greeting=你好 ; a=1</html>

addr = 'localhost',8003

@fastcgi(addr)
def hello(): sys.stdout.write(c())
time.sleep(0.5)
proc = subprocess.Popen('./http2fcgi -fcgi tcp://localhost:8003'.split())
t = _f()
time.sleep(0.2)
res = urlread('http://localhost:6065/setup.py?a=1', foo='bar', greeting='你好')
print(urllib.parse.unquote(res.decode()))
proc.terminate()