无法在Windows上编译一个简单的python脚本(使用py2exe)

时间:2016-06-01 12:27:48

标签: python windows py2exe paramiko

首先抱歉我的英语不好。

我制作了一个连接Mikortik RB750并通过SSH执行命令的脚本。 当我运行它(没有编译)时,它运行良好并符合目标。 我遇到的问题是,当我尝试在Windows上编译它失败时。在尝试运行SSH连接之前正常工作。

我尝试使用python -m py2exe.build_exe ssh_menu.py命令编译它 它告诉我缺少4个模块:UserDict; GSSAPI;打包; readline的。 我不知道如何解决这个问题,而且我谷歌很多,可怜这是我第一次尝试用python做点什么而且我现在迷路了。

这是我的代码:

import os 
import sys
import time
import select
import paramiko
server, username, password = ('192.168.88.1', 'admin', '976431')
comando1=" "
comando2=" "
ipUbicacion=" "
flagUbicacion=True
ipSalida=" "
flagSalida=True
addresList1=" "
miIp=input("Cual es su dirección IP: ")
while flagUbicacion:
    print("""
        1.ESSO Centro
        2.ESSO Rotonda
        3.Casa Don Oscar / Sebastian
        4.Transporte Garay
        5.Salir
    """)
idUbicacion=input("Cual es su ubicación? ")
if idUbicacion=="1":
  ipUbicacion="192.168.88.1"
  flagUbicacion=False
elif idUbicacion=="2":
  ipUbicacion="192.168.88.2"
  flagUbicacion=False
elif idUbicacion=="3":
  ipUbicacion="192.168.88.3"
  flagUbicacion=False
elif idUbicacion=="4":
  ipUbicacion="192.168.88.4"
  flagUbicacion=False
elif idUbicacion=="5":
  print("\n Programa Terminado") 
  idUbicacionans = None
else:
   print("\n El dato ingresado no es valido")
while flagSalida:
    print("""
    1.Modem ESSO Centro
    2.Integra ESSO Centro
    3.Modem ESSO Rotonda
    4.Modem Casa Don Oscar
    5.10+ Transporte Garay
    6.GampelCom Transporte Garay
    7.Salir
    """)
idSalida=input("Por cual puerta de enlace quiere salir? ")
if idSalida=="1":
  ipSalida="192.168.88.1"
  auxUbicacion="1"
  flagSalida=False
elif idSalida=="2":
  ipSalida="192.168.88.1"
  auxUbicacion="1"
  flagSalida=False
elif idSalida=="3":
  ipSalida="192.168.88.2"
  auxUbicacion="2"
  flagSalida=False
elif idSalida=="4":
  ipSalida="192.168.88.3"
  auxUbicacion="3"
  flagSalida=False
elif idSalida=="5":
  ipSalida="192.168.88.4"
  auxUbicacion="4"
  flagSalida=False
elif idSalida=="6":
  ipSalida="192.168.88.4"
  auxUbicacion="4"
  flagSalida=False
elif idSalida=="7":
  print("\n Programa Terminado") 
  idSalida = None
  flagSalida=False
else:
   print("\n El dato ingresado no es valido")
if ipUbicacion==ipSalida:
    cantComandos=1
else:
    cantComandos=2
i=0
while i<cantComandos:
    if idUbicacion=="1": #SI ESTOY EN LA ESSO DEL CENTRO Y...
        if idSalida=="1": #...QUIERO SALIR POR EL MODEM
            addresList1="WAN1"
        elif idSalida=="2": #...QUIERO SALIR POR EL INTEGRA
            addresList1="WAN2"
        elif idSalida=="3": #...QUIERO SALIR POR RB_ROTONDA
            addresList1="WAN3"
        elif idSalida=="4": #...QUIERO SALIR POR RB_OSCAR
            addresList1="WAN4"
        elif idSalida=="5": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN5"
        elif idSalida=="6": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN5"
    if idUbicacion=="2": #SI ESTOY EN LA ESSO DE LA ROTONDA Y...
        if idSalida=="1": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN2"
        elif idSalida=="2": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN2"
        elif idSalida=="3": #...QUIERO SALIR POR MODEM ROTONDA
            addresList1="WAN1"
        elif idSalida=="4": #...QUIERO SALIR POR RB_OSCAR
            addresList1="WAN3"
        elif idSalida=="5": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN4"
        elif idSalida=="6": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN4"
    if idUbicacion=="3": #SI ESTOY EN LA CASA DE OSCAR Y...
        if idSalida=="1": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN2"
        elif idSalida=="2": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN2"
        elif idSalida=="3": #...QUIERO SALIR POR RB_ROTONDA
            addresList1="WAN3"
        elif idSalida=="4": #...QUIERO SALIR POR MODEM OSCAR
            addresList1="WAN1"
        elif idSalida=="5": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN4"
        elif idSalida=="6": #...QUIERO SALIR POR RB_TRANSPORTE
            addresList1="WAN4"
    if idUbicacion=="4": #SI ESTOY EN TRANSPORTE Y...
        if idSalida=="1": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN3"
        elif idSalida=="2": #...QUIERO SALIR POR RB_CENTRO
            addresList1="WAN3"
        elif idSalida=="3": #...QUIERO SALIR POR RB_ROTONDA
            addresList1="WAN4"
        elif idSalida=="4": #...QUIERO SALIR POR RB_OSCAR
            addresList1="WAN5"
        elif idSalida=="5": #...QUIERO SALIR POR 10+ TRANSPORTE
            addresList1="WAN1"
        elif idSalida=="6": #...QUIERO SALIR POR GAMPELCOM TRANSPORTE
            addresList1="WAN2"
    if i==0:
        server=ipUbicacion
        while True:
            print ("Conectando a "+server+": Intento "+repr(i)+" de 10")
            try:
                ssh = paramiko.SSHClient()
                ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
                ssh.connect(server, username=username, password=password)
                print ("Conectado a "+server)
            break
            except paramiko.AuthenticationException:
                print ("Fallo de autentificación conectando a "+server)
            except:
                print ("Fallo de SSH en "+server+", esperando que se inicie")
            i += 1
            time.sleep(1)
        if i == 10:
            print ("No se pudo conectar a "+server+". Conexión cancelada") % server
    ssh.exec_command('ip firewall address-list remove [find where address="'+miIp+'"]')
    ssh.exec_command('ip firewall address-list add list="'+addresList1+'" address="'+miIp+'"')
    print('ip firewall address-list add list="'+addresList1+'" address="'+miIp+'"')
    print ("Comando ejecutado, cerrando conexión SSH")
    ssh.close()
    time.sleep(1)
else:
    server=ipSalida
    while True:
        print ("Conectando a "+server+": Intento "+repr(i)+" de 10")
        try:
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(server, username=username, password=password)
            print ("Conectado a "+server)
            break
        except paramiko.AuthenticationException:
            print ("Fallo de autentificación conectando a "+server)
        except:
            print ("Fallo de SSH en "+server+", esperando que se inicie")
            i += 1
            time.sleep(1)
        if i == 10:
            print ("No se pudo conectar a "+server+". Conexión cancelada") % server
    ssh.exec_command('ip firewall address-list remove [find where address="'+miIp+'"]')
    ssh.exec_command('ip firewall address-list add list="'+addresList1+'" address="'+miIp+'"')
    print('ip firewall address-list add list="'+addresList1+'" address="'+miIp+'"')
    print ("Comando ejecutado, cerrando conexión SSH")
    ssh.close()
    time.sleep(1)
idUbicacion=auxUbicacion
i=i+1

1 个答案:

答案 0 :(得分:0)

编译python程序时,我倾向于使用cxfreeze而不是py2exe(使用windows命令提示符中的pip install CX_Freeze命令),您可以使用此代码编译程序:

cxfreeze "Path\To\File.py" --target-dir "Path\to\exe\file\"

Path\To\File.py是您尝试编译的python文件的目录树

Path\to\exe\file\是存储已编译程序的文件夹的目录树(我建议将其设置为空)

有关详细信息,请参阅here

希望这有帮助。