0

我对后缀和 python 很陌生。我已经在 Ubuntu 上设置了 postfix 并配置main.cfmailbox_command = /home/someuser/test.py

测试.py:

#!/usr/bin/python
import sys, MySQLdb

email_input = sys.stdin
db = MySQLdb.connect(host="localhost",
                     user="user", 
                     passwd="password",
                     db="test")

cur = db.cursor()

sql = "insert into postfix (value) values (%s)"
cur.execute(sql, (email_input,))
db.commit()
db.close()

我希望将电子邮件的内容插入到该字段中,但结果却是<open file '<stdin>', mode 'r' at 0x7f018b3b40c0>

如何从似乎是该内存地址的地方获取原始电子邮件字符串?

4

1 回答 1

1

sys.stdin是一个类型的对象,TextIOWrapper需要cur.execute一个字符串。您需要指示sys.stdin读取输入并返回代表它的字符串。使用其中一个readlinereadlines取决于您要执行的操作。

cur.execute(sql, (sys.stdin.readlines(),))
于 2016-10-16T20:25:32.353 回答