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

Hoàng Tuấn Việt vietht2 at viettel.com.vn
Tue Nov 19 09:39:56 CET 2013


Hi Terry,

Thanks for your comment.

 

>To better help us help you, what exact version of Python?

I use Python 2.7.5

 

>Please post plain text without html.

Sorry, I will do it.

 

>Please post programs single spaced with just occasional blank lines.

 

File my_program.py

 

import wx.aui

import paramiko

import telnetlib

from LinuxHostPage import *

from WindowsHostPage import *

from AIXHostPage import *

Publisher = pub.Publisher()

TIMEOUT = 3

 

class MainWindow(wx.Frame):

    def __init__(self, parent, title):

        

        self.os = ''

        self.connection = []

        

        wx.Frame.__init__(self, parent, title=title)

        self.SetBackgroundColour('LightGray')

        

        # A Statusbar in the bottom of the window

        self.CreateStatusBar() 

        self.SetStatusText("Please input credentials.")

        

        # Publisher

        Publisher().subscribe(self.change_statusbar, 'change_statusbar')

        

        # Input box

        self.input_panel = wx.Panel(self)

        self.host = wx.StaticText(self.input_panel, -1, 'Host')

        self.host_entry = wx.TextCtrl(self.input_panel)

        self.username = wx.StaticText(self.input_panel, -1, 'Username')

        self.username_entry = wx.TextCtrl(self.input_panel)

        self.password = wx.StaticText(self.input_panel, -1, 'Password')

        self.password_entry = wx.TextCtrl(self.input_panel, style = wx.TE_PASSWORD)

        self.connect_btn = wx.Button(self.input_panel, -1, 'Connect')

        ## List box

        self.os_text = wx.StaticText(self.input_panel, -1, 'OS')

        self.choice_list = wx.Choice(self.input_panel, choices=['Redhat', 'SuSE', 'Windows', 'Solaris', 'AIX'])

        self.choice_list.SetSelection(2)

        

        self.input_sizer = wx.BoxSizer(wx.HORIZONTAL)

        self.input_sizer.Add((7,0))

        self.input_sizer.Add(self.os_text, flag = wx.ALIGN_CENTER)

        self.input_sizer.Add((7,0))

        self.input_sizer.Add(self.choice_list, flag = wx.ALIGN_CENTER)

        self.input_sizer.Add((7,0))

        self.input_sizer.Add(self.host, flag = wx.ALIGN_CENTER)

        self.input_sizer.Add((7,0))

        self.input_sizer.Add(self.host_entry, flag = wx.ALIGN_CENTER)

        self.input_sizer.Add((7,0))

        self.input_sizer.Add(self.username, flag = wx.ALIGN_CENTER)

        self.input_sizer.Add((7,0))

        self.input_sizer.Add(self.username_entry, flag = wx.ALIGN_CENTER)

        self.input_sizer.Add((7,0))

        self.input_sizer.Add(self.password, flag = wx.ALIGN_CENTER)

        self.input_sizer.Add((7,0))

        self.input_sizer.Add(self.password_entry, flag = wx.ALIGN_CENTER)

        self.input_sizer.Add((7,0))

        self.input_sizer.Add(self.connect_btn, flag = wx.ALIGN_CENTER)

        

        self.input_panel.SetSizer(self.input_sizer)

                       

        # Function panel

        self.notebook = wx.aui.AuiNotebook(self)

        

        # Top Sizer

        self.sizer = wx.BoxSizer(wx.VERTICAL)

        self.sizer.Add((0,7))

        self.sizer.Add(self.input_panel, border = 10)

        self.sizer.Add((0,7))

        self.sizer.Add(self.notebook, 1, wx.EXPAND)

        self.sizer.Add((0,7))

        

        self.SetSizerAndFit(self.sizer)

        

        # Show Frame

        self.Show()

 

        # Events

        self.connect_btn.Bind(wx.EVT_BUTTON, self.on_connect)

        self.Bind(wx.EVT_CLOSE, self.on_close)

        

    def change_statusbar(self, msg):

        self.SetStatusText(msg.data)

        

    def ssh(self, host, username, password):

        connection = paramiko.SSHClient()

        connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        connection.connect(host, 22, username, password)

        stdin, stdout, stderr = connection.exec_command('ls')

        stdin.write('expected_input\n')

        stdin.flush()

        return connection

    

    def telnet(self, host, os, username, password):

        connection = telnetlib.Telnet(host)

        connection.read_until('login: ')

        connection.write(username + '\r')

        connection.read_until('assword: ')

        connection.write(password + '\r')

        if os == 'windows':

            connection.read_until('>', timeout = TIMEOUT)

        elif os == 'aix':

            connection.read_until('#', timeout = TIMEOUT)

        return connection

    

    def get_os(self):

        os = self.choice_list.GetLabel()

        return os.lower()

    

    def on_connect(self, event):

        self.os = self.get_os()

        self.connect_btn.Disable()

        host = self.host_entry.GetValue()

        username = self.username_entry.GetValue()

        password = self.password_entry.GetValue()

        self.SetStatusText('Connecting... Please wait.')

        self.connect_btn.Disable()

            

        if self.os == 'redhat' or self.os == 'suse':

            self.connection = self.ssh(host, username, password)

            page = LinuxHostPage(self, self.connection, self.os, Publisher)

        elif self.os == 'windows':

            self.connection = self.telnet(host, self.os, username, password)

            page = WindowsHostPage(self, self.connection, self.os, Publisher)

        elif self.os == 'aix':

            self.connection = self.telnet(host, self.os, username, password)

            page = AIXHostPage(self, self.connection, self.os, Publisher)

            

        self.SetStatusText('Connected')

        self.connect_btn.Enable()

        # Connection tab

        self.notebook.AddPage(page, host, select = True)

                

    def close_connection(self):

        self.connection.close() 

        

    # Clean up and close

    def on_close(self, event):

        self.close_connection

        self.Destroy()

        

    def on_gen(self, event):

        # Validate inputs

        if self.src_input.GetLabel() == '' or self.protocol_input.GetLabel()=='':

            # Set status bar

            wx.MessageBox('Please input all required fields!', 'Info', wx.OK | wx.ICON_INFORMATION)

            return

        

        # Src IP

        src_ip = '-s ' + self.src_input.GetLabel()

 

        # Protocol

        proto = '-p ' + self.protocol_input.GetLabel().lower()

        

        # Src port

        src_port = self.src_port_input.GetLabel().lower().strip()

        if  src_port == '(optional)' or src_port == '':

            src_port = ''

        else:

            src_port = '--sport ' + src_port 

            

        # Dst port

        dst_port = self.dst_port_input.GetLabel().lower().strip()

        if  dst_port != '':

            dst_port = '--dport ' + dst_port 

            

        # Target

        target = self.target_input.GetLabel().strip()

        if target == '(optional)' or target == '':

            target = '-j ACCEPT'

        else:

            target = '-j ' + target.upper()

            

        # Rule

        if src_port == '':

            rule = '{:10s}{:30s}{:10s}{:20s}{:22s}{:20s}'.format('-A INPUT', src_ip, proto, dst_port, '-m state --state NEW', target)

        else:

            rule = '{:10s}{:30s}{:10s}{:20s}{:20s}{:22s}{:20s}'.format('-A INPUT', src_ip, proto, src_port, dst_port, '-m state --state NEW', target)

        

        self.rule.Clear()

        self.rule.WriteText(rule)

        self.rule.Enable()

        

        # Set status bar

        self.set_status_bar('Generated rule successfully.')

        

    def set_status_bar(self, str):

        Publisher().sendMessage(('change_statusbar'), str)

            

app = wx.App(False)

frame = MainWindow(None, "Firewall Management Tool")

frame.Maximize()

app.MainLoop()

 

 

I can run the program in Eclipse and telnet successfully to a Windows host.

 

But when I export to .exe file by py2exe:

 

python gen_exe.py py2exe

 

 

File gen_exe.py:

 

from distutils.core import setup

import py2exe

 

setup(

    options = {

            "py2exe":{

            "packages": ['wx.lib.pubsub'],

            "dll_excludes": ["MSVCP90.dll", "HID.DLL", "w9xpopen.exe"],

        }

    },

    console = [{'script': ‘my_program.py'}]

)

 

When I run my_program.exe, this error is displayed:

 

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

 

at line:

 

       connection.write(username + '\r')

 

--

Viet

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131119/ffe23275/attachment.html>


More information about the Python-list mailing list