R
R
run4way2015-11-09 21:42:11
Python
run4way, 2015-11-09 21:42:11

Python: how to process cyrillic from GET?

Good afternoon. There is a simple script, one part passes data from the form via GET. The other parses the url and takes this data from there. The problem is that Cyrillic characters are not processed as expected - upper does not work, for example. And if earlier I defeated this with the help of decode ('utf-8'), then on the server neither decode nor encode, nor other methods help.
The host is located at OpenShift.

Linux version 2.6.32-573.7.1.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16)

LANG=en_US.UTF-8

Python 2.7.8

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os

virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
    pass
#
# IMPORTANT: Put any additional includes below this line.  If placed above this
# line, it's possible required libraries won't be in your searchable path
#
import urlparse
def application(environ, start_response):

    ctype = 'text/plain'
    if environ['PATH_INFO'] == '/health':
        response_body = "1"
    elif environ['PATH_INFO'] == '/env':
        response_body = ['%s: %s' % (key, value)
                    for key, value in sorted(environ.items())]
        response_body = '\n'.join(response_body)
    elif environ['PATH_INFO'] == '/' or environ['PATH_INFO'] == '/main':
        ctype = 'text/html'
        response_body = '''<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Welcome to OpenShift</title>
</head>
<body>
    <form action="/justdoit" method="get">
        <input type="text" name="name">
        <input type="text" name="code">
        <input type="submit" value="Send">
    </form>
</body>
</html>'''

    elif environ['PATH_INFO'] == '/justdoit':
        ctype = 'text/html'
        params = urlparse.parse_qs(environ.get('QUERY_STRING','utf-8'))
        name = params.get('name', [''])[0]
        code = str(params.get('code', [''])[0])

.......................... далее код опущен

    status = '200 OK'
    response_headers = [('Content-Type', ctype), ('Content-Length', str(len(response_body)))]
    start_response(status, response_headers)
    return [response_body]

#
# Below for testing only
#
if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    httpd = make_server('localhost', 8051, application)
    # Wait for a single request, serve it and quit.
    httpd.handle_request()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-11-09
@sim3x

It will be easier to put jung or a flask and do everything with them. It is
better to throw the script itself away as a whole - it is not clear what it does

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question