shalabh / quixote_extras

User contributed add-ons and for Quixote. See http://mems-exchange.org/software/quixote/. This repository used to be hosted at http://www.cafepy.com/quixote_extras/. All links from old location are now redirected here.

Clone this repository (size: 917.6 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/shalabh/quixote_extras/
commit 149: 5b7911ccd017
parent 148: fbb05270db35
branch: default
tags: tip
Updated readme to reflect move to bitbucket.org
Shalabh Chaturvedi
3 months ago
quixote_extras / rex / cgi2scgi.py
r149:5b7911ccd017 37 loc 983 bytes embed / history / annotate / raw /
#!/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),