Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'skip:[ 20': 0.04; 'yet.': 0.04; 'encoding': 0.05; 'error:': 0.07; "subject:' ": 0.07; 'username,': 0.07; 'string': 0.09; 'ascii': 0.09; 'encode': 0.09; 'host,': 0.09; 'idea?': 0.09; 'line:': 0.09; 'subject:position': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'windows': 0.15; 'bit.': 0.16; 'cc:name:python list': 0.16; 'codec': 0.16; 'debugged': 0.16; 'eclipse': 0.16; 'fabio': 0.16; 'non-ascii': 0.16; 'ordinal': 0.16; 'reproduce': 0.16; 'subject: \n ': 0.16; 'timeout': 0.16; 'timeout)': 0.16; '\xc2\xa0i': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'properly': 0.19; 'select': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'byte': 0.24; 'unicode': 0.24; 'mon,': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'options': 0.25; '>': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'file:': 0.31; 'os,': 0.31; 'username': 0.31; 'skip:c 30': 0.32; 'run': 0.32; 'url:python': 0.33; 'guess': 0.33; 'problem': 0.35; "can't": 0.35; 'common': 0.35; 'connection': 0.35; 'skip:u 20': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'nov': 0.38; 'skip:[ 10': 0.38; 'skip:& 20': 0.39; 'subject:can': 0.39; 'url:mail': 0.40; 'skip:u 10': 0.60; 'skip:\xc2 10': 0.60; 'skip:* 10': 0.61; "you're": 0.61; 'different': 0.65; 'skip:\xe2 10': 0.65; 'here': 0.66; 'internet': 0.71; '8bit%:100': 0.72; 'export': 0.74; 'behavior': 0.77; '8bit%:16': 0.84; '8bit%:70': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=HQpPipBbhcf/S7Jh+AWdf+cnEpTq4p60jgfAUDC38KE=; b=fUEBtRuy1ORrA4hfHQc16ycBc+ozOhOYH51sFOMlDo+je1rTnd0otJD/e4J8FZn1IT XJMXng88NZF/7HWg2JHgxLfknMjaadJZmVJaUzIFyH3ck87citWe+8X4zGqHV0B2pH// vIoMRLW7CwRiGL7UYR9VWLX7vcEz8Ic7LTwf0/ZDh4a9DBKxenr/eiAmuE3s5T7dW6Y+ fvAEc1yWzVFX8ID8wH0+z8XTcZuMZbwJs6/+laBJFOlPaoR82EmAsRT+rXrb0pPMZJo/ +HTyHlS7yBpVQRaEdC072xpxxo0uT1PVImzurBjg1so5vPvtac4BQAYH+nFl6k040ZWG G1zw== X-Received: by 10.220.58.1 with SMTP id e1mr15326162vch.0.1384775157758; Mon, 18 Nov 2013 03:45:57 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <002501cee41a$6d8ccdf0$48a669d0$@viettel.com.vn> References: <002501cee41a$6d8ccdf0$48a669d0$@viettel.com.vn> From: Fabio Zadrozny Date: Mon, 18 Nov 2013 09:45:37 -0200 Subject: Re: UnicodeDecodeError: 'ascii' codec can't decodee byte 0xff in position 0: ordinal not in range(128) To: =?UTF-8?B?SG/DoG5nIFR14bqlbiBWaeG7h3Q=?= Content-Type: multipart/alternative; boundary=001a11c2c7da5f750e04eb721773 Cc: python list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 325 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384775167 news.xs4all.nl 15956 [2001:888:2000:d::a6]:54312 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59854 --001a11c2c7da5f750e04eb721773 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Mon, Nov 18, 2013 at 2:55 AM, Ho=C3=A0ng Tu=E1=BA=A5n Vi=E1=BB=87t wrote: > Hi all, > > > > I use Python telnetlib on Windows 7 32 bit. Here is my code: > > > > def *telnet*(*self*, host, os, username, password): > > connection =3D telnetlib.Telnet(host) > > connection.read_until(*'login: '*) > > connection.write(username + *'\r'*) > > connection.read_until(*'assword: '*) > > connection.write(password + *'\r'*) > > connection.read_until(*'>'*, timeout =3D TIMEOUT) > > return connection > > > > I can run the program in Eclipse and telnet successfully to a Windows hos= t. > > > > But when I export to .exe file: > > > > from distutils.core import setup > > import py2exe > > > > setup( > > options =3D { > > *"py2exe"*:{ > > *"packages"*: [*'wx.lib.pubsub'*], > > *"dll_excludes"*: [*"MSVCP90.dll"*, *"HID.DLL"*, > *"w9xpopen.exe"*], > > } > > }, > > console =3D [{*'script'*: *=E2=80=98my_program.py'*}] > > ) > > > > and run the programe, I encounter this error: > > > > UnicodeDecodeError: 'ascii' codec can't decodee byte 0xff in position 0: > ordinal not in range(128) > > > > at line: > > > > connection.write(username + '\r') > > > > I have debugged and searched the Internet hard but found no solution yet. > > > > I think it is because of =E2=80=98\r=E2=80=99. > > > > Do you have any idea? > > > > Viet > > -- > https://mail.python.org/mailman/listinfo/python-list > > You should be able to reproduce the same behavior on PyDev if in your run configuration you select the encoding of the console to be ascii (run > run configurations > select run configuration > common > set encoding to us-ascii). My guess is that you have the problem because the username has non-ascii chars -- and you're receiving it as an unicode and not a string... so, you have to do encode it properly to a string before writing to the connection (i.e.: username.encode('utf-8') + '\r' -- although the encoding may have to be a different one and not utf-8). Cheers, Fabio --001a11c2c7da5f750e04eb721773 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable





On Mon, Nov 18, 2013 at 2:55 AM, Ho= =C3=A0ng Tu=E1=BA=A5n Vi=E1=BB=87t <vietht2@viettel.com.vn> wrote:

Hi all,

=C2=A0

I use Python telnetlib on Windows 7 32 bit. Here is= my code:

=C2=A0

=

=C2=A0=C2=A0=C2=A0 def telnet(self, host, os, userna= me, password):

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 co= nnection =3D telnetlib.Telnet(host)

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 co= nnection.read_until('login: ')

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 connection.write(username + '\r')

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 co= nnection.read_until('assword: ')

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 co= nnection.write(password + '\r')

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 co= nnection.read_until('>', timeout =3D TIMEOUT)

= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return connection

=C2=A0

I can run the program in Eclipse and telnet successful= ly to a Windows host.

=C2=A0=

But when I export to .exe file:

= =C2=A0

from distutils.core import setup

import py2exe

=C2=A0

setup(

=C2=A0=C2=A0=C2=A0 options =3D {

=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "py2exe":{

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 "packages": ['wx.lib.pubsub'= ],

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 "dll_excludes": ["MSVCP90.dll&= quot;, "HID.DLL", "w9xpopen.exe"],

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<= /span>

=C2=A0=C2=A0=C2=A0 },

=C2=A0=C2=A0=C2=A0 console =3D [{'script': =E2=80=98my_program.py'}]

= )

= =C2=A0

and run the programe, I enco= unter this error:

=C2=A0

=

UnicodeDecodeError: 'ascii' codec can't = decodee byte 0xff in position 0: ordinal not in range(128)

=C2=A0

<= p class=3D"MsoNormal"> at line:

=C2=A0

connection.w= rite(username + '\r')

=C2=A0

= I have debugged and searched the Internet hard but found no solution yet.

=C2=A0

I thi= nk it is because of =E2=80=98\r=E2= =80=99.

=C2=A0=

Do you have any idea?

=C2=A0

Viet<= u>


--
https://mail.python.org/mailman/listinfo/python-list


You should be a= ble to reproduce the same behavior on PyDev if in your run configuration yo= u select the encoding of the console to be ascii (run > run configuratio= ns > select run configuration > common > set encoding to us-ascii)= .

My guess is that you have the problem because the username h= as non-ascii chars -- and you're receiving it as an unicode and not a s= tring... so, you have to do encode it properly to a string before writing t= o the connection (i.e.: username.encode('utf-8') + '\r' -- = although the encoding may have to be a different one and not utf-8).

Cheers,

Fabio
--001a11c2c7da5f750e04eb721773--