python - 如何将用户输入自动传递给Python脚本?

标签 python django

Possible Duplicate:
how to write in stdout after to call a method (doing a system of notifications automatic (Iphone))

我有以下脚本,我想在 django 应用程序中自动执行。问题是它不断要求用户输入。

我想知道如何自动传递用户输入?这是脚本:

from apns import APNs, Payload

apns = APNs(use_sandbox=True, cert_file='cert.pem', key_file='key.pem')

# Send a notification
token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87'
payload = Payload(alert="Hello World!", sound="default", badge=1)
my_pass_phrase = 'efisgbieafb3939bg93g238g4792gff9'
apns.gateway_server.send_notification(token_hex, payload)

问题发生在以下行:

apns.gateway_server.send_notification(token_hex, payload)

脚本要求:输入 PEM 密码短语:并等待用户输入。

我想知道如何自动传递密码。提前致谢!

最佳答案

因为您正在使用库,并且可能无法直接访问来禁止请求用户输入,所以您可能需要在调用此脚本的代码中使用 pexpect 模块观察输出中的特定模式,然后适本地发送输入:

http://pypi.python.org/pypi/pexpect-u/2.5.1

示例:http://www.noah.org/wiki/pexpect

可能是这样的:

import pexpect

child = pexpect.spawn('myAPNscript.py')
child.expect('Enter PEM pass phrase:.*')
child.sendline('FOO')

关于python - 如何将用户输入自动传递给Python脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12429503/

相关文章:

python - 枚举A盒中N个球的组合?

python - 用python数据框中的新结尾替换单词结尾

javascript - Django:随机函数调用导致管道中断

python - SSL:CERTIFICATE_VERIFY_FAILED 尝试使用 django 验证 reCAPTCHA

python - 在 Django 模板 forloop 中迭代调用列表的索引

python - 解析引号和转义字符 CSV 文件

Python 颜色图

django - 在 django 中运行syncdb时找不到关系

python - django Rest Framework - 如何为序列化器添加静态字段值

python - Celery 工作人员在 Matplotlib subplot() 期间过早退出