Skip to content

Instantly share code, notes, and snippets.

@KhorAMus
Created April 15, 2016 17:39
Show Gist options
  • Save KhorAMus/da7fb1dc752b6e6bed3c9267725446e3 to your computer and use it in GitHub Desktop.
Save KhorAMus/da7fb1dc752b6e6bed3c9267725446e3 to your computer and use it in GitHub Desktop.
В 17-ой строке мы вставляем в ответ кодировку, иначе при обращении к полю text объекта Response
выбросится ошибка: AttributeError: You cannot access Response.text unless charset is set. Обращение к нему происходит в функции print.
Исходный код программы на Python 3.4
from webob import Request, Response
environ = {
"SERVER_NAME": "e1.ru",
"SERVER_PORT": "80",
"HTTP_INFO": 'e1.ru:80',
"PATH_INFO": '/news/',
'REQUEST_METHOD': "GET",
'SCRIPT_NAME': '',
'USER_AGENT': "Mozilla/5.0 (x11; U; Linux i686; ru;rv:1.9b5)G Firefox/3.0b5",
'Accept': 'text/html',
'wsgi.url_scheme': 'http'
}
req = Request(environ)
resp = req.get_response()
resp.charset = 'windows-1251'
print(resp)
Результат работы программы:
301 Moved Permanently
Server: nginx
Date: Fri, 15 Apr 2016 16:00:31 GMT
Content-Length: 178
Connection: keep-alive
Location: http://www.e1.ru/news/
Content-Type: text/html; charset=windows-1251
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment