Search code examples
djangopython-2.7utf-8big5

'ascii' codec can't decode byte 0xb7 in position 0: ordinal not in range(128)


I am trying to send sms in my django project,this is my sms function

mit2sms.py

import urllib
import urllib2



def sendsms(phonenumber,textcontent):
    textcontent=textcontent.decode('utf8').encode('big5') #10
    #textcontent=textcontent.encode('big5')  #9
    url = "https://urls?username=myname&password=mypassword&dstaddr="+phonenumber+"&smbody="+textcontent
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)

I can send the sms in Chinese&English in python shell ok.But when I import it to my django project I just got

'ascii' codec can't decode byte 0xb7 in position 0: ordinal not in range(128)

error

my django

views.py

#-*- coding: utf-8 -*-
....
from mit2sms import *   # I import the sms function here
....

def register(request):
    ....
    tel = request.POST['tel']
    textcontent = "會員申請通過"
    sendsms(tel,textcontent)
    ....

views.py and mit2sms.py are in same directory

but after I registered I got error

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/register/

Django Version: 1.9.7
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'member']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/Users/korekyourin/books/stayreal/sandbox/member_register/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/Users/korekyourin/books/stayreal/sandbox/member_register/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/korekyourin/books/stayreal/sandbox/member_register/member_register/member/views.py" in register
  57.     sendsms(tel,textcontent)

File "/Users/korekyourin/books/stayreal/sandbox/member_register/member_register/member/mit2sms.py" in sendsms
  10.     url = "https://urls?username=myname&password=mypassword&dstaddr="+phonenumber+"&smbody="+textcontent

Exception Type: UnicodeDecodeError at /register/
Exception Value: 'ascii' codec can't decode byte 0xb7 in position 0: ordinal not in range(128)

Solution

  • You tend to get UnicodeDecodeError UnicodeDecodeError: 'ascii' codec can't decode when you implicitly convert a str to a Unicode.

    This can happen under the following conditions:

    unicode('€')                       # explicit conversion without encoding
    u"The currency is: {}".format('€') # new style format string into Unicode string - Python will try to convert value string to Unicode first
    u'The currency is: %s' % '€'       # old style format string into Unicode string - Python will try to convert value string to Unicode first
    u'The currency is: ' + '€'         # append string to Unicode - Python will try to convert string to Unicode first
    

    The latter is probably what you're hitting - appending an str to a Unicode. Python will attempt to convert the str to a Unicode first, but will only use the "ascii" codec (Python 2.x), which will fail for non-ascii contents.

    It's not immediately obvious but it's very likely that you're getting a Unicode object from:

    tel = request.POST['tel']
    

    Then when you add the tel/phonenumber (a Unicode object) to textcontent (your big5 encoded str), you will get the UnicodeDecodeError.

    The answer is to remove any implied conversion and remove unnecessary decode/encodes.

    Change:

    textcontent = u"會員申請通過" # Note the 'u`. textcontent is now a Unicode
    

    and change sendsms() to:

    def sendsms(phonenumber,textcontent):
        url = u"https://urls?username=myname&password=mypassword&dstaddr="+phonenumber+"&smbody="+textcontent
        # now a unicode also
        # better written using String.format(): 
        # url = u"https://urls?username=myname&password=mypassword&dstaddr={phonenumber}&smbody={textcontent}".format(phonenumber=phonenumber, textcontent=textcontent)
        req = urllib2.Request(url.encode('big5')) # encode only when absolutely necessary
        response = urllib2.urlopen(req)