[英]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.