在服务器上无限运行python脚本

在服务器上无限运行python脚本

本文介绍了在服务器上无限运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个需要在服务器上无限运行的python脚本.它将访问Microsoft Exchange服务器并阅读邮件,对其进行处理并触发自动语音呼叫.

I am building a python script which needs to run infinitely on a server. It will access a Microsoft Exchange server and read mails, process them and trigger automated voice calls.

我已经成功实施了自动呼叫操作.目前,脚本在我的PC上运行.我有三个问题.

I have successfully implemented the automated call action. Presently the script runs on my PC. I have three questions.

  1. 要在服务器而不是PC上运行脚本,是否需要更改除连接到服务器之外的其他代码的语法?我的意思是,我正在阅读邮件和触发呼叫的部分需要更改吗?还是可以在服务器上运行相同的脚本?如果确实需要更改,请有人附上需要更改的内容.
  2. 由于我需要在服务器上运行脚本并访问Microsoft Exchange服务器,因此脚本可以在Exchange服务器本身上运行吗?如果是,请附上有用的资源.
  3. 该脚本本身不接受任何输入,但是它会访问几个需要不时手动编辑的文件.我应该如何实现?

推荐答案

  1. PC和服务器之间的区别无关紧要.您的脚本将需要一组资源,并且可能会对其运行的操作系统进行假设.这些才是最重要的.只要有所需的资源,它就可以正常运行.例如,如果您的脚本要求运行Python 3.6+,则必须在服务器/PC上安装Python 3.6+.如果您使用的是特定的python软件包,则应安装该软件包.如果您假设文件在磁盘上的位置,则这些路径要么必须独立于操作系统,要么与服务器/PC的操作系统相匹配,并且这些文件必须位于此处.但是python的语法不应更改.

如果您的目标是在服务器上将python服务器作为服务运行,则需要有关哪种服务器类型(windows/linux)的更多信息.假设您正在考虑在交换服务器上运行它,我想您很可能希望在Windows上运行它.如何在此处提出并回答了.关于代码,您将要确保可以将脚本作为库处理,并且不想在代码内部调用 sys.exit ,但应依靠异常来传递错误.我的首选模式是类似

If your goal is to run the python server as a service on the server, then more information about what type of server (windows/linux) is required. Assuming you are considering running it on an exchange server, I suppose it's most likely you'll want to run on Windows. This has been asked and answered here. In relation to your code, you will want to make sure your script can be handled as a library, and you won't want to call sys.exit inside your code, but should rely on exceptions to pass up errors. My preferred pattern is something like

def main(argv=None):
  # parse arguments if you have them and run the script

if __name__ == '__main__':
  main()

然后在服务中,您可以导入并调用 main(...),而无需运行其他可执行文件.

Then in your service you can import and call main(...) without running another executable.

  1. 请参阅#1.它是否可以在该服务器上运行取决于所有必需的资源和文件是否在该服务器上可用.可能存在一个问题,您是否要在交换服务器上运行该脚本.答案取决于脚本承担的负载,服务器的繁忙/活动状态,是否要在服务器上安装其他软件等等.

  1. See #1. Whether it can run on that server depends on whether all of the required resources and files are available there. There is possibly a question of whether you would WANT to run the script on your exchange server. That answer depends on the load the script takes, how busy/active your server is, Whether you want the extra software installed on your server, etc.

您在这里的最佳解决方案将取决于您的情况.如果您可以登录和编辑文件,那么也许就是您要做的.如果要在PC上对其进行编辑然后将其向上推,则有相应的解决方案.一切取决于您的项目/情况的合理选择.

Your best solution here will depend on your situation. If you can login and edit the files, then maybe that's what you do. If you want to edit them on your PC and then push them up, then there are solutions for that. All depends on what makes sense for your project/situation.

这篇关于在服务器上无限运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 22:34