|
| 1 | +import fastpysgi |
| 2 | + |
| 3 | +def app(environ, start_response): |
| 4 | + path = environ["PATH_INFO"] |
| 5 | + req_method = environ.get('REQUEST_METHOD', '') |
| 6 | + |
| 7 | + if path == '/cookie': |
| 8 | + cookie_str = environ.get('HTTP_COOKIE', '') |
| 9 | + lines = [ ] |
| 10 | + for pair in cookie_str.split(';'): |
| 11 | + pair = pair.strip() |
| 12 | + eq = pair.find('=') |
| 13 | + if eq > 0: |
| 14 | + lines.append(f"{pair[:eq]}={pair[eq+1:]}") |
| 15 | + body = '\n'.join(lines) + '\n' if lines else '' |
| 16 | + start_response('200 OK', [ ('Content-Type', 'text/plain') ]) |
| 17 | + return [ body.encode() ] |
| 18 | + |
| 19 | + if path == '/echo': |
| 20 | + lines = [ ] |
| 21 | + for key, value in environ.items(): |
| 22 | + header_name = None |
| 23 | + if key.startswith('HTTP_'): |
| 24 | + header_name = key[5:] |
| 25 | + if key == 'CONTENT_TYPE': |
| 26 | + header_name = 'Content-Type' |
| 27 | + if key == 'CONTENT_LENGTH': |
| 28 | + header_name = 'Content-Length' |
| 29 | + if header_name: |
| 30 | + header_name = header_name.replace('_', '-').title() |
| 31 | + lines.append(f"{header_name}: {value}") |
| 32 | + body = '\n'.join(lines) + '\n' |
| 33 | + start_response('200 OK', [ ('Content-Type', 'text/plain') ]) |
| 34 | + return [ body.encode() ] |
| 35 | + |
| 36 | + if req_method == 'POST': |
| 37 | + try: |
| 38 | + length = int(environ.get('CONTENT_LENGTH', 0) or 0) |
| 39 | + except ValueError: |
| 40 | + length = 0 |
| 41 | + body = environ['wsgi.input'].read(length) if length > 0 else b'' |
| 42 | + start_response('200 OK', [ ('Content-Type', 'text/plain') ]) |
| 43 | + return [ body ] |
| 44 | + |
| 45 | + start_response('200 OK', [ ('Content-Type', 'text/plain') ]) |
| 46 | + return [ b'OK' ] |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + import sys |
| 50 | + port = int(sys.argv[1]) if len(sys.argv) > 1 else 8080 |
| 51 | + fastpysgi.run(app, host="0.0.0.0", port=port, loglevel=0) |
0 commit comments