简体   繁体   English

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

[英]Failing compiling on windows a simple python script (using py2exe)

First of all sorry for my bad English. 首先,抱歉我的英语不好。

I made a script that connects to a Mikortik RB750 and executes commands by SSH. 我制作了一个脚本,该脚本连接到Mikortik RB750并通过SSH执行命令。 When I run it (without compiling), it works well and meets the objective. 当我运行它(无需编译)时,它运行良好并达到了目标。 The problem I'm having is that when I try to compile it on Windows fails. 我遇到的问题是,当我尝试在Windows上编译时失败。 Works fine until it try to run the SSH connection. 正常运行,直到尝试运行SSH连接。

I'm trying to compile it using the command python -m py2exe.build_exe ssh_menu.py It tells me that 4 modules are missing: UserDict; 我正在尝试使用命令python -m py2exe.build_exe ssh_menu.py进行编译。它告诉我缺少4个模块:UserDict; gssapi; gssapi; packaging; 打包; readline. 阅读线。 I have no idea how to solve this, and I google a lot, pitifully this is the first time that I attempt to do something with python and I'm lost now. 我不知道如何解决这个问题,我在Google上搜索了很多,可惜这是我第一次尝试用python做某事,但现在我迷路了。

Here is my code: 这是我的代码:

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

When compiling a python program, I tend to use cxfreeze instead of py2exe (use the command pip install CX_Freeze from the windows command prompt), where you can use this code to compile the program: 在编译python程序时,我倾向于使用cxfreeze而不是py2exe(使用Windows命令提示符下的pip install CX_Freeze命令),您可以在其中使用以下代码来编译程序:

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

Path\\To\\File.py is the directory tree to the python file you are trying to compile Path\\To\\File.py是您尝试编译的python文件的目录树

Path\\to\\exe\\file\\ is the directory tree to a folder where the compiled program will be stored (I recommend this to be empty) Path\\to\\exe\\file\\是将存储已编译程序的文件夹的目录树(我建议将此为空)

See here for more details. 有关更多详细信息,请参见此处

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM