~zethra/stargazer

ref: fbbf828c33087d1601d81db7c99c41d71fcb930f stargazer/scripts/scgi-server -rwxr-xr-x 1.1 KiB
fbbf828c — Ben Aaron Goldberg config: add bad config for testing 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3

import sys
import socketserver

class SCGIServer(socketserver.BaseRequestHandler):
    def handle(self):
        self.request.sendall(b"20 text/plain\r\n")
        self.request.sendall(b"Hello world!")
        buf = ""
        while True:
            b = self.request.recv(1)
            if b == b':':
                break
            buf += chr(ord(b))
        l = int(buf)
        buf = self.request.recv(l)
        state = True
        part = ""
        out = ""
        for b in buf:
            if b == 0:
                if state:
                    out += f"{part}: "
                else:
                    out += f"{part}\n"
                state = not state
                part = ""
            else:
                part += chr(b)
        lines = out.split("\n")
        lines.sort()
        out = "\n".join(lines)
        self.request.sendall(bytes(out, 'utf-8'))
        self.request.close()


if __name__ == "__main__":
    try:
        with socketserver.TCPServer(("localhost", 1099), SCGIServer) as server:
            server.serve_forever()
    except KeyboardInterrupt:
        exit()