【问题标题】:convert python script to exe and run as windows service将python脚本转换为exe并作为windows服务运行
【发布时间】:2016-05-17 03:55:09
【问题描述】:

我刚刚创建了一个 python 脚本来解决我需要的问题,但我想将此脚本转换为 exe 文件以在任何 Windows 机器上运行它,而无需在其上安装 python 我搜索了如何将 py 转换为 exe 并运行它,我发现我可以使用名为 py2exe 的脚本这里的问题是我想将我的文件转换为 exe 并在我的 PC 上连续作为 Windows 服务运行.

这是我的脚本:

import socket, sys, serial

HOST = ''   # Symbolic name, meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

#Start listening on socket
s.listen(10)
print 'Socket now listening'

# try:

#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    # print('Connected with {}:{}'.format(addr[0], addr[1]))
    str = conn.recv(100)
    n_str = str[8:]
    last_c = n_str.find('%')
    last_str = n_str[:last_c]
    final_str = last_str.replace('+',' ')[:-3]
    print(final_str)
    try:
        pole = serial.Serial('COM4')
        pole.write('                                          \r\n')
        pole.write(final_str+'\r\n')
        pole.close()
    except:
        print(Exception.message)



s.close()

我可以在这里得到一些帮助

【问题讨论】:

  • Windows 服务有一些特殊要求。请参阅stackoverflow.com/q/32404/291641 以获得正确方法的答案。你不能只是让任何旧的可执行文件成为服务。

标签: python windows python-2.7 openerp py2exe


【解决方案1】:

Python 是一种解释型语言,而不是编译型语言。因此,它需要它的解释器才能被执行。

记住这一点,你可以使用这个:http://www.py2exe.org

此处提供更多选项:a good python to exe compiler?

甚至更好,在这里:https://wiki.python.org/moin/DistributionUtilities

【讨论】:

  • 你的意思是即使我把它转换成exe我也必须安装python才能运行它?
  • 要么,要么包必须包含执行.exe的所有内容,必须包含文件运行所需的所有内容。什么都不会“嵌入到文件中”。好吧,理论上,它可以嵌入,但是你会有一个非常大的文件。 Mac 中的应用程序就是一个很好的例子。当你打开容器时,你可以看到里面的一切!这就是为什么 Python 解释器预装在每个操作系统上的原因(当然,除了好的旧 Windows)!
  • 好的,你能告诉我如何将那个 exe 作为服务运行吗?我更新了我的代码
  • 我可以,但是 Python 文档可以以更连贯的方式做到这一点。这是最重要的部分,但我建议您阅读整个文档:docs.python.org/3.5/faq/…
  • @MostafaMohamed,如果这回答了您的问题,请将其标记为这样,以便对其他人也有用。 :) 否则,请详细说明!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
  • 2017-08-01
  • 2021-06-12
  • 2020-11-23
相关资源
最近更新 更多