import socket,struct
def enc(p):
    r=b''
    for k,v in p.items():
        for b in (k.encode(),v.encode()):
            if len(b)<128: r+=struct.pack('B',len(b))
            else: r+=struct.pack('!I',len(b)|0x80000000)
        r+=k.encode()+v.encode()
    return r
p={'GATEWAY_INTERFACE':'CGI/1.1','REQUEST_METHOD':'GET','SCRIPT_FILENAME':'/tmp/t1.php','SCRIPT_NAME':'/t1.php','REQUEST_URI':'/t1.php','QUERY_STRING':'','PATH_INFO':'','PATH_TRANSLATED':'/tmp/t1.php','SERVER_PROTOCOL':'HTTP/1.1','REMOTE_ADDR':'127.0.0.1','REMOTE_PORT':'50000','SERVER_ADDR':'127.0.0.1','SERVER_PORT':'80','SERVER_NAME':'localhost','SERVER_SOFTWARE':'nginx/1.22','DOCUMENT_ROOT':'/tmp','HTTP_HOST':'localhost','REDIRECT_STATUS':'200','PHP_VALUE':'display_errors=1&error_reporting=32767&log_errors=0','PHP_ADMIN_VALUE':'display_errors=1&error_reporting=32767'}
out=struct.pack('!BBHH',1,1,1,8)+struct.pack('!HB',1,0)
pd=enc(p)
out+=struct.pack('!BBHH',1,4,1,len(pd))+pd
out+=struct.pack('!BBHH',1,4,1,0)
out+=struct.pack('!BBHH',1,5,1,0)
s=socket.create_connection(('127.0.0.1',9000),4)
s.settimeout(8)
s.sendall(out)
data=b''
while True:
    try: c=s.recv(65536)
    except socket.timeout: break
    if not c: break
    data+=c
print('total',len(data))
i=0
while i+8<=len(data):
    v,t,rid,l = data[i],data[i+1],struct.unpack('!H',data[i+2:i+4])[0],struct.unpack('!H',data[i+4:i+6])[0]
    pad=data[i+6]
    chunk=data[i+8:i+8+l]
    print('type',t,'len',l)
    print(' ',repr(chunk[:500]))
    i+=8+l+pad
