使用 os.system 运行 Twilio CLI 命令会导致错误

Posted

技术标签:

【中文标题】使用 os.system 运行 Twilio CLI 命令会导致错误【英文标题】:Run Twilio CLI command using os.system results in error 【发布时间】:2022-01-16 11:11:26 【问题描述】:

我有一个 Twilio CLI 命令,我希望在 Raspberry Pi 上使用 Python os.system() 运行它,但是它不能正常运行...说它遇到错误。

我也看不懂调试信息...

当前代码如下所示:

command_string = '/home/pi/.config/nvm/versions/node/v16.13.1/bin/twilio phone-numbers:update ' +my_number+ ' --sms-url=http://localhost:5000 -l debug'

os.system(command_string)

Python 脚本运行良好,但我不断收到上述命令的以下结果:

[DEBUG] Config File: /root/.twilio-cli/config.json
[DEBUG] Using profile: $TWILIO_ACCOUNT_SID/$TWILIO_AUTH_TOKEN
[DEBUG] -- BEGIN Twilio API Request --
[DEBUG] GET https://api.twilio.com/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/IncomingPhoneNumbers.json
[DEBUG] Querystring:
[DEBUG] "PhoneNumber":"NUMBER"
[DEBUG] Custom HTTP Headers:
[DEBUG] User-Agent: twilio-cli/2.35.0 @twilio/cli-core/5.32.2 (linux arm) twilio-node/3.71.3 node/v16.13.0 phone-numbers:update
[DEBUG] -- END Twilio API Request --
[DEBUG] response.statusCode: 200
[DEBUG] response.headers: "date":"Sun, 12 Dec 2021 19:03:24 GMT","content-type":"application/json; charset=utf-8","content-length":"1585","connection":"close","twilio-concurrent-requests":"1","twilio-request-id":"RQ1c8c8551e33b2b8efaf2a3e93ce84050","twilio-request-duration":"1.085","access-control-allow-origin":"*","access-control-allow-headers":"Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since","access-control-allow-methods":"GET, POST, DELETE, OPTIONS","access-control-expose-headers":"ETag","access-control-allow-credentials":"true","x-powered-by":"AT-5000","x-shenanigans":"none","x-home-region":"us1","x-api-domain":"api.twilio.com","strict-transport-security":"max-age=31536000"
[DEBUG] Found command "phone-numbers:update" plugin: twilio-cli
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=info msg="no configuration paths supplied"
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=info msg="ignoring default config path, could not stat it" path=/root/.ngrok2/ngrok.yml err="stat /root/.ngrok2/ngrok.yml: no such file or directory"
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=info msg="starting web service" obj=web addr=127.0.0.1:4040
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=info msg=start pg=/api/tunnels id=9ba04b3a3b59fd8f
t=2021-12-13T03:03:25+0800 lvl=warn msg="ngrok is not yet ready to start tunnels" pg=/api/tunnels id=9ba04b3a3b59fd8f err="a successful ngrok tunnel session has not yet been established"
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=info msg=end pg=/api/tunnels id=9ba04b3a3b59fd8f status=503 dur=1.646498ms
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=info msg=start pg=/api/tunnels id=38085fdc657c6c64
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=warn msg="ngrok is not yet ready to start tunnels" pg=/api/tunnels id=38085fdc657c6c64 err="a successful ngrok tunnel session has not yet been established"
t=2021-12-13T03:03:25+0800 lvl=info msg=end pg=/api/tunnels id=38085fdc657c6c64 status=503 dur=829.471µs
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=info msg=start pg=/api/tunnels id=1d29d31d96b0c316
t=2021-12-13T03:03:25+0800 lvl=warn msg="ngrok is not yet ready to start tunnels" pg=/api/tunnels id=1d29d31d96b0c316 err="a successful ngrok tunnel session has not yet been established"
t=2021-12-13T03:03:25+0800 lvl=info msg=end pg=/api/tunnels id=1d29d31d96b0c316 status=503 dur=494.549µs
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=info msg=start pg=/api/tunnels id=541085ca341447c1
[DEBUG] ngrok: t=2021-12-13T03:03:25+0800 lvl=warn msg="ngrok is not yet ready to start tunnels" pg=/api/tunnels id=541085ca341447c1 err="a successful ngrok tunnel session has not yet been established"
t=2021-12-13T03:03:25+0800 lvl=info msg=end pg=/api/tunnels id=541085ca341447c1 status=503 dur=900.748µs
[DEBUG] ngrok: t=2021-12-13T03:03:26+0800 lvl=info msg=start pg=/api/tunnels id=894e9ee1a0583b59
[DEBUG] ngrok: t=2021-12-13T03:03:26+0800 lvl=info msg="tunnel session started" obj=tunnels.session
t=2021-12-13T03:03:26+0800 lvl=info msg="client session established" obj=csess id=fe7aff6afc5d
[DEBUG] ngrok: t=2021-12-13T03:03:26+0800 lvl=warn msg="failed to start tunnel" pg=/api/tunnels id=894e9ee1a0583b59 err="You must signup for ngrok and add your authtoken to perform this operation.\n\nSign up for an account: https://dashboard.ngrok.com/signup\nInstall your authoken: https://dashboard.ngrok.com/get-started/your-authtoken\r\n\r\nERR_NGROK_4018\r\n"
[DEBUG] ngrok: t=2021-12-13T03:03:26+0800 lvl=info msg=end pg=/api/tunnels id=894e9ee1a0583b59 status=502 dur=391.989355ms
[DEBUG] Found command "phone-numbers:update" plugin: twilio-cli
 » twilio-cli encountered an unexpected error. To report this issue, execute the command with the "-l debug" flag, then copy the output to a new issue here: "https://github.com/twilio/twilio-cli/issues"
[DEBUG] Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property '_httpMessage' -> object with constructor 'ClientRequest'
    --- property 'socket' closes the circle
[DEBUG] TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property '_httpMessage' -> object with constructor 'ClientRequest'
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at NumberUpdate.createTunnel (/home/pi/.config/nvm/versions/node/v16.13.1/lib/node_modules/twilio-cli/src/commands/phone-numbers/update.js:94:51)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async NumberUpdate.run (/home/pi/.config/nvm/versions/node/v16.13.1/lib/node_modules/twilio-cli/src/commands/phone-numbers/update.js:48:9)
    at async NumberUpdate._run (/home/pi/.config/nvm/versions/node/v16.13.1/lib/node_modules/twilio-cli/node_modules/@oclif/command/lib/command.js:43:20)
    at async Config.runCommand (/home/pi/.config/nvm/versions/node/v16.13.1/lib/node_modules/twilio-cli/node_modules/@oclif/config/lib/config.js:173:24)
    at async Main.run (/home/pi/.config/nvm/versions/node/v16.13.1/lib/node_modules/twilio-cli/node_modules/@oclif/command/lib/main.js:27:9)
    at async Main._run (/home/pi/.config/nvm/versions/node/v16.13.1/lib/node_modules/twilio-cli/node_modules/@oclif/command/lib/command.js:43:20)

因此,我无法接收来自 Twilio 的短信...

你知道如何进行这项工作吗?

【问题讨论】:

【参考方案1】:

这里是 Twilio 开发者宣传员。

这里的输出中有一个来自 ngrok 的错误:

You must signup for ngrok and add your authtoken to perform this operation.

Sign up for an account: https://dashboard.ngrok.com/signup
Install your authoken: https://dashboard.ngrok.com/get-started/your-authtoken

ERR_NGROK_4018

所以,看来您需要sign up for a free ngrok account 和provide your authtoken。


您似乎在围绕 Python 和 Twilio CLI 编排此应用程序时遇到了一些问题。您可能会发现这完全在 python 中更容易处理。您可以结合使用 Twilio Python library 和 ngrok Python library 来启动隧道并在应用程序中更新电话号码的 URL。

【讨论】:

嗨,我有一个帐户,可以从我的终端执行ngrok http 5000,但无法使用 os.system() 运行此命令。我不确定是什么问题或可以找到任何解决方案... ngrok 通常将身份验证令牌保存在~/.ngrok2/ngrok.yml 的文件中。在您的情况下,它看起来应该是/root/.ngrok2/ngrok.yml。我猜你自己的用户目录中有一个.ngrok2 目录,但没有root 用户目录,所以看看你是否可以复制它。 感谢您的帮助!我让它工作,但结果不是我所期望的。因此,我决定使用 ngrok Python 库。然而,这开始给我带来一些问题。我 pip 卸载了 pyngrok 并且问题仍然存在。我不确定问题是 pyngrok 还是我的程序。该问题已使用测试程序在此处发布:***.com/questions/70326878/…。任何帮助将不胜感激【参考方案2】:

这里是 ngrok 的产品经理。为了遏制对平台的滥用,我们现在要求所有提供 html 内容的用户拥有一个免费帐户。您将需要在 ngrok.com 上注册一个免费帐户,并将您的 authtoken 添加到您的 ngrok 配置文件或作为 Phil 描述的命令行标志,即使您使用其中一个 ngrok 库也是如此。如果您有任何问题或疑虑,请联系 ngrok 支持!谢谢!

【讨论】:

以上是关于使用 os.system 运行 Twilio CLI 命令会导致错误的主要内容,如果未能解决你的问题,请参考以下文章

Python之os.popen和os.system

用批处理文件bat 运行的py文件, 无法使用 os.system语句

python 的os.system问题?

如何结束作为 os.system() 调用运行的线程(Python)

通过 os.system 推送

Python:运行 os.system 后如何获取标准输出? [复制]