简体   繁体   English

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

[英]Running python script infinitely on a server

I am building a python script which needs to run infinitely on a server.我正在构建一个需要在服务器上无限运行的 python 脚本。 It will access a Microsoft Exchange server and read mails, process them and trigger automated voice calls.它将访问 Microsoft Exchange 服务器并阅读邮件、处理邮件并触发自动语音呼叫。

I have successfully implemented the automated call action.我已经成功实现了自动呼叫操作。 Presently the script runs on my PC.目前该脚本在我的 PC 上运行。 I have three questions.我有三个问题。

  1. For running the script on a server instead of PC, does the syntax of the code other than connecting to the server needs to change?为了在服务器而不是 PC 上运行脚本,除了连接到服务器之外,代码的语法是否需要更改? I mean, the parts where I'm reading mails and triggering calls, does that need to be changed?我的意思是,我正在阅读邮件和触发呼叫的部分,是否需要更改? Or can the same script run on a server?还是可以在服务器上运行相同的脚本? If it does need change, can somebody please attach what changes need to be done.如果确实需要更改,有人可以附上需要做的更改。
  2. Since I need to run the script on a server, and access a Microsoft Exchange server, can the script be run on the Exchange server itself?由于我需要在服务器上运行脚本并访问 Microsoft Exchange 服务器,那么脚本可以在 Exchange 服务器本身上运行吗? If yes, please attach helpful resources.如果是,请附上有用的资源。
  3. The script does not take any input as such, but it accesses a couple of files that need to edited manually from time to time.该脚本不接受任何输入,但它会访问一些需要不时手动编辑的文件。 How should I achieve that?我应该如何做到这一点?
  1. The distinction between PC and Server doesn't matter. PC 和服务器之间的区别并不重要。 Your script will require a set of resources and may make assumptions about the OS it's running on.您的脚本将需要一组资源,并且可能会对其运行的操作系统做出假设。 Those are the things that matter.这些才是最重要的。 As long as the required resources are there, it should run fine.只要有所需的资源,它就应该运行良好。 For example, if your script requires Python 3.6+ to run, then you must have Python 3.6+ installed on either the Server/PC.例如,如果您的脚本需要 Python 3.6+ 才能运行,那么您必须在服务器/PC 上安装 Python 3.6+。 If you are using a particular python package, then it should be installed.如果您使用的是特定的 python package,则应该安装它。 If you make assumptions about where files are located on disk, those paths either need to be OS independent, or match the OS of the Server/PC, and those files need to be there.如果您假设文件在磁盘上的位置,这些路径要么需要独立于操作系统,要么与服务器/PC 的操作系统匹配,并且这些文件需要在那里。 But the syntax of the python shouldn't change.但是 python 的语法不应该改变。

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.如果您的目标是将 python 服务器作为服务器上的服务运行,则需要有关服务器类型(windows/linux)的更多信息。 Assuming you are considering running it on an exchange server, I suppose it's most likely you'll want to run on Windows.假设您正在考虑在交换服务器上运行它,我想您很可能希望在 Windows 上运行。 This has been asked and answered here . 这已被问及回答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.关于您的代码,您需要确保您的脚本可以作为库处理,并且您不想在代码中调用sys.exit ,而是应该依靠异常来传递错误。 My preferred pattern is something like我喜欢的模式是

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

if __name__ == '__main__':
  main()

Then in your service you can import and call main(...) without running another executable.然后在您的服务中,您可以导入并调用main(...)而不运行另一个可执行文件。

  1. See #1.见#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.该答案取决于脚本的负载、服务器的繁忙/活跃程度、是否要在服务器上安装额外的软件等。

  2. 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.如果您想在 PC 上编辑它们然后将它们向上推送,那么有解决方案。 All depends on what makes sense for your project/situation.一切都取决于对您的项目/情况有意义的事情。

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

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