我正在尝试将一个SMTP实时服务器作为django中的单独应用程序。每当我尝试运行smtp文件python smtp.py
来监听传入的消息并将它们存储在模型中时,我都会收到ImproperlyConfigured错误。
smtp.py
import os
import smtpd
import asyncore
import datetime
from models import IncomingEmail
from email.parser import Parser
class MessageServer():
def __call__(self, peer, mailfrom, recipients, data):
email = Parser().parsestr(data)
incomingDb = IncomingEmail(
sender=mailfrom, recipient=recipients,
message=data, subject=email['subject'],
recevied=datetime.datetime.now()
)
incomingDb.save()
class customSMTPServer(smtpd.SMTPServer):
process_message = MessageServer()
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings")
server = customSMTPServer(('localhost', 1025), None)
print "Started listening at port 1025"
try:
asyncore.loop()
except KeyboardInterrupt:
server.close()
项目结构:
hello/
manage.py
hello/
__init__.py
settings.py
urls.py
wsgi.py
smtp/
smtp.py
models.py
__init__.py
api/
admin.py
apps.py
urls.py
views.py
错误:
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environ
ment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
答案 0 :(得分:1)
看起来你收到了这个错误,因为DB engin缺少一些重要的设置。 如果您编辑这样的代码怎么办?
import sys, os
sys.path.append('/path/to/your/app')
os.environ['DJANGO_SETTINGS_MODULE'] = 'hello.settings'
from django.conf import settings
print "Started listening at port 1025"
...
或者另一种方法是尝试在文件顶部移动os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings")
(当然,在导入os
之后)
另一个有点脏的黑客是:
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
接下来你可以试试:
$ ./manage.py shell
...
>>> execfile('your_script.py')
另外,哪一行抛出异常?