Quixote Extras
hist

<root> / rex / cgi2scgi.py

#!/usr/bin/env python
"""cgi2scgi.py -- Send a request to an SCGI server.
"""
import os, socket, sys

HOST = 'localhost'
PORT = 3000

def scgi_request(content, headers_dict):
    dic = {'SCRIPT_NAME': '', 'PATH_INFO': '/'}
    dic.update(headers_dict)
    headers = [
        ('CONTENT_LENGTH', len(content)),
        ('SCGI', 1)]
    headers += dic.items()
    headers = ["%s\0%s\0" % pair for pair in headers]
    headers = ''.join(headers)
    headers_len = len(headers)
    return "%s:%s,%s" % (headers_len, headers, content)

def recieve_all(sock):
    sio = StringIO()
    while 1:
        data = sock.recv(1024)
        if not data:
            break
        sio.write(data)
    return sio.getvalue()

def send_scgi(content, headers_dict):
    data = scgi_request(content, headers_dict)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((HOST, PORT))
    sock.send(data)
    return recieve_all(sock)

print send_scgi(sys.stdin.read(), os.environ),