From php called a bash script, and everything was fine
ob_implicit_flush(true); ob_end_flush(); system ("sudo /path/start.sh $name_user 2>&1");
The script was run on the server side without any problems, but for a long time.
I rewrote it in python, the script from the console works fine and displays Russian letters.
But if you call it from php:
header( "Content-Type: text/html; charset=utf-8" ); <code> ob_implicit_flush(true); ob_end_flush(); echo "<pre>"; system ("sudo /path/start.py $name_user 2>&1");
That error falls out:
Traceback (most recent call last): File "/path/start.py", line 250, in print ("\ u041f \ u043e \ u0435 \ u0445 \ u0430 \ u043b \ u0438)") UnicodeEncodeError: 'ascii' codec can ' t encode characters in position 0-6: ordinal not in range (128)
Error on the first occurring print with Russian characters.
In the bash script, at the beginning, I prescribed forcibly that the UTF locale, since despite the header, the variables do not go as planned, but there is an error even to variables. Even if all the transmitted variables lead to Latin characters, it swears on Russian words in the script itself.
What to do and how to specify the forced encoding?
Script tried on python2 with # - - coding: utf - - (# - - coding: utf-8 - -)
I also took python3, which is already normal with Russian letters, still the problem remains. At the same time everything works fine from the console.
Of course, there is a desire to rewrite the entire php in python, but nevertheless, the question remains and the problem has not yet been resolved.
How to be?
Added later:
Something is not yet possible to achieve a result, maybe I am not looking there or doing something wrong?
but added PYTHONIOENCODING to the global / etc / profile environment
export PYTHONIOENCODING="UTF-8"
The file itself was made with such content.
#!/usr/bin/env python3.4 # -*- coding: utf -*- import os #print("Русский текст весь") print(os.environ['PYTHONIOENCODING'])
When calling from php
system ("sudo /path/start.py 2>&1");
Mistake
Traceback (most recent call last): File "/etc/openvpn/easy-rsa/easyrsa3/vpn.py", line 7, in print(os.environ['PYTHONIOENCODING']) File "/usr/lib64/python3.4/os.py", line 633, in getitem raise KeyError(key) from None KeyError: 'PYTHONIOENCODING'
Same result if
system ("PYTHONIOENCODING=utf-8 && sudo /path/start.py 2>&1");
This is the third python, and if the second is python, then there are no problems with Russian letters now.
It turns out, as it was written above, that Python3 takes its encoding, but if it is explicitly given, will utf-8 not use it anyway?
Added by:
I also tried this code
envname = "PYTHONIOENCODING" print("{}:\t{}".format(envname, os.environ.get(envname))) for set_locale in [False]: print("locale({}):\t{}".format(set_locale, locale.getpreferredencoding(set_locale))) for streamname in "stdout stderr stdin".split(): stream = getattr(sys, streamname) print("device({}):\t{}".format(streamname, os.device_encoding(stream.fileno()))) print("{}.encoding:\t{}".format(streamname, stream.encoding)) for set_locale in [False, True]: print("locale({}):\t{}".format(set_locale, locale.getpreferredencoding(set_locale)))
console output received
PYTHONIOENCODING: UTF-8 locale(False): UTF-8 device(stdout): UTF-8 stdout.encoding: UTF-8 device(stderr): UTF-8 stderr.encoding: UTF-8 device(stdin): UTF-8 stdin.encoding: UTF-8 locale(False): UTF-8 locale(True): UTF-8 Поехали )
A Favorite
PYTHONIOENCODING: None locale(False): ANSI_X3.4-1968 device(stdout): None stdout.encoding: ANSI_X3.4-1968 device(stderr): None stderr.encoding: ANSI_X3.4-1968 device(stdin): None stdin.encoding: ANSI_X3.4-1968 locale(False): ANSI_X3.4-1968 locale(True): ANSI_X3.4-1968 Traceback (most recent call last): File "/etc/openvpn/easy-rsa/easyrsa3/vpn.py", line 261, in print ("\u041f\u043e\u0435\u0445\u0430\u043b\u0438 )") UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-6: ordinal not in range(128)
That is, the encoding is not transmitted as
I tried to add
sys.stdout.buffer.write((" Русский ").encode('utf8'))
And in the browser there is no error and the Russian text appeared ... but do not write the same there for every meeting predlozheniya?