@maximifn
Пока ты спишь - твой враг качается

Как принять кириллицу в GET запросе Flask?

Кто сталкивался с проблемой раскодирования текста на русском GET-запроса? Код
# !/usr/bin/python
# -*- coding: utf8 -*-
# import os, sys
# импортируем нужные модули
import json, urllib2, time
from flask import Flask, request

app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def yaapi_bot():
    # указываем адрес для json запросов
    url = 'https://api-sandbox.direct.yandex.ru/live/v4/json/'
    phrases_ = (request.args.get('keyword'))
    geo_id = request.args.get('geo_id')
    # указываем токен доступа
    token = 'ТУТ ТОКЕН'

Ошибка:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

Возможно стоит использовать какой то метод декодирования?
Еще код ошибки
GET /?keyword=%D0%BF%D0%BE%D0%BC%D0%B8%D0%B4%D0%BE%D1%80%D1%8B%20%D1%81%D0%BF%D0%B1&geo_id=0 HTTP/1.1" 500

Использую python2

Полный трейбек ошибки:
[2018-01-26 15:45:23,469] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "C:\env_for_pc\venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\env_for_pc\venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\env_for_pc\venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\env_for_pc\venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\env_for_pc\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:/env_for_pc/env_for_pc.py", line 113, in yaapi_bot
  File "C:/env_for_pc/env_for_pc.py", line 37, in makeRep
    response = urllib2.urlopen(url, jdata)
  File "C:\Python27\Lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\Lib\urllib2.py", line 391, in open
    response = self._open(req, data)
  File "C:\Python27\Lib\urllib2.py", line 409, in _open
    '_open', req)
  File "C:\Python27\Lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python27\Lib\urllib2.py", line 1181, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "C:\Python27\Lib\urllib2.py", line 1142, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "C:\Python27\Lib\httplib.py", line 946, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\Lib\httplib.py", line 987, in _send_request
    self.endheaders(body)
  File "C:\Python27\Lib\httplib.py", line 940, in endheaders
    self._send_output(message_body)
  File "C:\Python27\Lib\httplib.py", line 807, in _send_output
    self.send(message_body)
  File "C:\Python27\Lib\httplib.py", line 775, in send
    self.sock.sendall(str)
  File "C:\Python27\Lib\ssl.py", line 205, in sendall
    v = self.send(data[count:])
  File "C:\Python27\Lib\ssl.py", line 176, in send
    v = self._sslobj.write(data)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 107-110: ordinal not in range(128)
127.0.0.1 - - [26/Jan/2018 15:45:23] "GET /?keyword=%D1%82%D0%B5%D1%81%D1%82 HTTP/1.1" 500 -
  • Вопрос задан
  • 671 просмотр
Пригласить эксперта
Ответы на вопрос 1
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы