/
zOMGdev
/
cppcms
Обзор
Документация
Войти
/
zOMGdev
/
cppcms
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
tests/toscgi.py
24 строки
788 B
Artyom Beilis
Python3 compatibility
26 авг 2020, 00:16
26 авг 2020, 00:16
2fc7e38
Код
Авторство
О чём код?
#!/usr/bin/env python # coding=UTF-8 # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 def toscgi(text): def to_headers(): res = [] hdr = text[:text.find(b'\n\n')] for element in hdr.split(b'\n'): [key,value]=element.split(b':') res.append((key.upper().replace(b'-',b'_'),value)) return res def to_body(): return text[text.find(b'\n\n')+2:] def format_headers(headers): result = b'' for [key,value] in headers: result+=key+b'\0'+value+b'\0' return result headers=to_headers() body=to_body() headers = [(b'CONTENT_LENGTH',str(len(body)).encode())] + headers headers=format_headers(headers) return str(len(headers)).encode()+b':' + headers +b',' + body