从Azure WebJob运行Python脚本

2024-05-12 20:54:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从azurewebjob运行python脚本。这是我在这之后所做的link

  1. 通过url https://<webapp name>.scm.azurewebsites.net访问kudu工具,并通过Site Extensions选项卡安装Python 364x86
  2. 已确认Python 364x86安装在以下路径:D:\home\python364x86
  3. D:\home\python364x86中添加了我的脚本trading.py
  4. 用这行代码创建了run.bat文件D:\home\python364x86\python.exe trading.py
  5. 在webjob zip文件中包含run.battrading.py
  6. 已部署,但出现错误
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Initializing
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Run script 'run.bat' with script host - 'WindowsScriptHost'
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Running
[09/07/2019 07:02:00 > 0dd02c: ERR ] The filename, directory name, or volume label syntax is incorrect.
[09/07/2019 07:02:00 > 0dd02c: INFO] 
[09/07/2019 07:02:00 > 0dd02c: INFO] D:\local\Temp\jobs\triggered\z\2az54ret.wh4>D:\home\python364x86\python.exe trading.py 
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Failed
[09/07/2019 07:02:00 > 0dd02c: SYS ERR ] Job failed due to exit code 1

在函数.cs在

^{pr2}$

上面的代码执行python脚本。它在本地工作,但一旦我将其部署到生产环境中,它就会失败。试了这么多次,花了太多的时间,但仍然不确定为什么prod不起作用。感谢帮助。在

在交易.py在

import telegram

my_token = 'mytoken'
bot = telegram.Bot(token = my_token)

chat_id = 'mychatid'
message = 'Hello
bot.sendMessage(chat_id=chat_id, text=message)

Tags: torunpyinfo脚本tokenidhome
1条回答
网友
1楼 · 发布于 2024-05-12 20:54:40

我尝试在WebJob中使用Python实现您的需求,并成功地使其工作。在

这是我的步骤和示例代码。我的本地环境是Windows10上的Python3.7。在

  1. 创建一个Python虚拟环境并通过如下命令安装python-telegram-bot包。在

    $ mkdir telegram-webjob
    $ virtualenv telegram-webjob
    $ cd telegram-webjob
    $ Scripts\active
    $ pip install python-telegram-bot
    $ pip freeze
    

    enter image description here

  2. 我更改了您的代码作为我的示例代码,然后在本地运行它很好,如下所示。在

    ^{pr2}$

    enter image description here

    enter image description here

  3. 我创建了一个名为webjob的新directoy,并将我的trading.py文件及其所有目录及其文件复制到其中,如下所示。在

    enter image description here

  4. 编写名为run.bat的启动bat脚本,代码如下。在

    D:\home\python364x86\python.exe trading.py
    

    D:\home\python364x86\python.exe路径是python364x86站点扩展的安装路径,如下图所示。在

    enter image description here

  5. 然后,我将webjob目录中的所有目录和文件打包为zip文件,如下所示。在

    enter image description here

  6. 我把它作为一个webjob上传到azurewebapp,如下图所示,然后启动它。在

    enter image description here

    enter image description here

最后,它对我很好,我可以看到消息间隔10秒显示在我的电报客户端。在

enter image description here

相关问题 更多 >