简体   繁体   English

我想用启动命令运行我的python程序(不是脚本),这是要执行的python程序文件中的一种方法

[英]i want to run my python program(not script) with start command which is a method in that python program file to be executed

Hi have python program in which a start method is defined, in start method i am calling a win32serviceutil.StartService(service) method to start a service, like 嗨,有一个python程序,其中定义了一个启动方法,在启动方法中,我正在调用win32serviceutil.StartService(service)方法来启动服务,例如

import os, platform, subprocess

try:
    import win32serviceutil
except:
    os.system("pip install pywin32")
    os.system("pip install pypiwin32")
    import win32serviceutil

OS = platform.system() #will get you the platform/OS
print("You are using ", OS)

if __name__=='__main__':

    service = 'WSearch'
    def startByCLI():
        cmd = 'net start '+service
        os.system(cmd)

    def startByPython():
        # subprocess.check_output(["sc", "start", service], stderr=subprocess.STDOUT)
        win32serviceutil.StartService(service)

    if OS=='Windows':
        try:
            output = startByPython()
        except subprocess.CalledProcessError as e:
            print(e.output)
            print(e.returncode)
            #os.system('python test2.py')
            subprocess.call("python ./install.py asadmin", shell=True)
            startByCLI()

so what i actually want is i want to run the start method from command promt like this 所以我真正想要的是我想像这样从命令promt运行start方法

python ./myfile.py startByPython

and it will trigger the startByPython method in myfile.py 它会触发startByPython的方法myfile.py

many thanks in advance 提前谢谢了

Hey all thanks for your attention, i wanted to run my file.py file with argument from command line like: 嘿,谢谢您的关注,我想使用来自命令行的参数来运行file.py文件,例如:

$ /usr/bin/python myfile.py start $ / usr / bin / python myfile.py开始

i got the solution which is 我得到的解决方案是

  def main(): # read arguments from the command line and # check whether at least two elements were entered if len(sys.argv) < 2: print "Usage: python aws.py {start|stop}\\n" sys.exit(0) else: action = sys.argv[1] 
   if action == "start":
      startInstance()
   elif action == "stop":
      stopInstance()
   else:
      print "Usage: python aws.py {start|stop}\n"

暂无
暂无

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

相关问题 当我使用Process.Start运行python程序时,我的python程序中的日志不起作用吗? - When I run a python program using Process.Start the log in my python program does not work? 在python中运行脚本程序 - run script program in python 我想在我的电脑启动时运行我的程序 - I want to run my program while my pc start 从我的Python脚本内部,我想安排另一个Python脚本或命令行脚本的延迟运行 - From inside my Python script, I want to schedule a deferred run of another Python script or a command line script 我试图让我的程序创建一个文件来保存输入,但我不希望它出现两次。 (Python) - I'm trying to get my program to create a file which will save an input, but I don't want it to appear twice. (Python) 为什么我的Python程序无法正常运行? 它可以运行,但没有得到我想要的结果 - Why my Python program can't run correctly? It can run but don't get the result I want to 使用命令行参数运行Python程序的Shell脚本 - Shell script to run Python program with command line arguments Python程序没有被执行 - Python program not getting executed 如何获取 python 脚本以在 Windows (10) 中运行命令 shell (10),它在程序特定位置打开? - How do I get a python script to run a command shell in Windows (10) where it opens in a program specific location? Python 在启动时运行套接字和我想要的程序的 rest - Python run socket at startup and the rest of the program when i want
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM