ASCII codec error Can't upload files django apache and redhat - django

When I tried to upload a file, my page shows this:
'ascii' codec can't encode character u'\xc1' in position 51: ordinal not in range(128)
os.stat(path)
u'/opt/djangoproject/ConvocatoriaESCA/media_cdn/Luis \xc1ngel Garc\xeda Ramos/Comprobante_Ingl\xe9s_Luis_\xc1ngel_Garc\xeda_Ramos.pdf'
My path to upload Files in models.py have:
def upload_location_comprobante_ingles(instance, filename):
filename = u"Comprobante_Inglés %s %s.pdf" % (instance.nombre, instance.apellidos)
return u"%s %s/%s" % (instance.nombre, instance.apellidos, filename)
and nombre and apellidos are UTF-8 strings

The problem is not in os.stat(path)!!
There are two ways how to deploy a django project using Apache.
Basic configuration
Daemon mode
In the both cases mod_wsgi is used.
By default httpd sets environment variable LANG=C. Thus the file system encoding will set to ascii.
You can checking it using sys.getfilesystemencoding(), passed its output to HttpResponse somewhere in your views (or write to a log).
To change LANG environment variable which httpd sets to a mod_wsgi process(es) open file /etc/sysconfig/httpd (on Red Hat based OSes). Then comment LANG=C or change it to your flavour. This approach will work both in Basic configuration and in Daemon mode.
In addition, Daemon mode supports the 'lang' option of the WSGIDaemonProcess command. In this case you will be able to set different a LANG to different virtualhosts.

Related

I am not able to write a file in NFS which has some chinese character in file name

Basically, when user uploads the file I am writing that file into NFS. But it gives an error 'ascii' codec can't encode characters in position 28-32: ordinal not in range(128)
I have tried to install Chinese locale and set it but it is not working
I am taking the file name as this but it is not working
file_name = file_name.encode('ascii').decode('unicode-escape')
We were having the same issue on our Debian 11 server running Apache2. Our environment locale was UTF-8 but when testing it showed up on the browser as ASCII.
In our case, the solution was to change the /etc/apache2/envvars file and remove the # before . /etc/default/locale.
We found this solution at itekblog.com/ascii-codec-cant-encode-characters-in-position/

selenium web driver not working

When i run the code, browser is opening but not at all loading anything.
i want to login into website using selenium.
im using windows 7 64 bit os and python 2.7.
i have downloaded drivers from https://github.com/mozilla/geckodriver/releases
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
path="D:\New folder (2)\geckodriver-v0.20.0-win64/geckodriver.exe"
driver = webdriver.Firefox(executable_path=path)
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
screen shot
when i close the browser it shows this error
error
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 0
You main issue is in the GeckoDriver absolute path which you have assigned to path.
You need to pass the argument executable_path along with the absolute path of the GeckoDriver binary through either of the following options :
Double back slashes (\\)
Single back slash (\) along with the raw (r) switch.
So you have to change the line :
path="D:\New folder (2)\geckodriver-v0.20.0-win64/geckodriver.exe"
To either :
path="D:\\New folder (2)\\geckodriver-v0.20.0-win64\\geckodriver.exe"
Or :
path=r'D:\New folder (2)\geckodriver-v0.20.0-win64\geckodriver.exe'

Save Permissions Python

I am writing a program which tracks pay stub information. It runs fine when running it from source code via the terminal and saves files correctly. In order to distribute it to my client, I compiled the code using PyInstaller to create a 1 file .exe for distribution on Windows 7, and then used Advanced Installer 11.4.1 to create a .msi file for them to install on their platform.
My problem is when running the application after installing the .msi. In the package I distributed a .txt file with the data to load and save to.
When attempting to update the file I distributed the following error occurs:
IOError: [Errno 13] Permission denied: 'testSave.txt'
The code I'm using to try and save the file is:
saving = open(file_name, 'w')
saving.write(data)
Is there a way to tell Python 2.7 to write regardless of privileges, or to make this specific file have basic user privileges when installing?
Thanks.
I modified my program to write to a different location using the system environment and telling Advanced Installer to install the .txt in the Local App Data directory. This allowed me to open the file with 'rw' privileges.
path = os.environ.get('LOCALAPPDATA')
path = path.split('\\')
real_path = ''
print path
for dir in path:
real_path += dir + '/'
print real_path
real_path = os.path.normpath(real_path + 'PayTrakker/testSave.txt')
With Advanced Installer you can set permissions, but you need a licensed edition, i.e. Professional or higher.
You can also build a Professional project in the trial period, to test the permissions support and see if it will work for you.

Django WSGI Python Encoding

I have a strange issue with my Django Apache Wsgi setup. I recently moved my site to a different server. Unfortunately now the encoding is somewhat messed up. When I run a command like this:
barcode.generate_barcode("ean", "1341341234234")
the resulting image will show some special character between every digit, typically for some endocing issue. I guess it is using two bytes instead of one to represent each char or something similar.
If I run the same setup with django ./manage.py runserver comand. The resulting image is fine, no special characters added.
So I came to the conclusion this must be something with my apache2/mod_wsgi setup. But the versions are identical to my old setup, i.e. debian 6, apache2.2, mod_wsgi 3.3.2.
I would like to try a newer version of mod_wsgi but compilation fails.
Can anyone point me in the right direction to where this encoding error might have its cause? To my understanding the WSGI context somehow loads the entire python script with a wrong encoding, otherwise I cannot explain why a hardcoded string would turn out wrong on an image.
In Apache conf.d/charset:
AddDefaultCharset UTF-8
In my vhost additionally:
AddDefaultCharset UTF-8
In apache2/envvars:
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
Envvars is used in apache runlevel script:
if [ -z "$APACHE_ENVVARS" ] ; then
APACHE_ENVVARS=$APACHE_CONFDIR/envvars
fi
I also tried to hardcode overwrite env vars in the runlevel script.
In my wsgi file:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
But nothing seems to help. My image is still generated with broken encoding.
An upgrade of mod_wsgi to version 3.4.0 fixed it.
You can use the option lang or locale to the WSGIDaemonProcess clause.
See in https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html

How can i use a commandlinetool (ie. sox) via subprocess.Popen with mod_wsgi?

I have a custom django filefield that makes use of sox, a commandline audiotool. This works pretty well as long as i use the django development server. But as soon as i switch to the production server, using apache2 and mod_wsgi, mod_wsgi catches every output to stdout. This makes it impossible to use the commandline tool to evaluate the file, for example use it to check if the uploaded file really is an audio file like this:
filetype=subprocess.Popen([sox,'--i','-t','%s'%self.path], shell=False,\
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(filetype,error)=filetype.communicate()
if error:
raise EnvironmentError((1,'AudioFile error while determining audioformat: %s'%error))
Is there a way to workaround for this?
edit
the error i get is "missing filename". I am using mod_wsgi 2.5, standard with ubuntu 8.04.
edit2
What exactly happens, when i call subprocess.Popen from within django in mod_wsgi? Shouldn't subprocess stdin/stdout be independent from django stdin/stdout? In that case mod_wsgi should not affect programms called via subprocess. Is it possible to use a commandlinetool like that from mod_wsgi?
Add debug to your program to log to stderr the value of 'self.path' to ensure it is actually set to something. The message 'missing filename' suggest it may be empty. Also be aware that when running on Apache/mod_wsgi you must use absolute path names to files because the current working directory will not be the project directory like with the Django development server. Finally, Apache runs as a special user, so it needs to have appropriate read and/or write access to the directories you need it to access/write to. The path and access issues are documented in:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues
BTW, for stdout issues, you really should upgrade to mod_wsgi 3.3. Read:
http://blog.dscpl.com.au/2009/04/wsgi-and-printing-to-standard-output.html