【发布时间】:2017-10-21 00:34:48
【问题描述】:
我希望用户使用我们学院的邮件服务器登录我的 django 网络应用程序。 但是这段代码允许在本地主机上使用学院邮件登录,而不是在服务器上。
我试过pythonanywhere django提供的shell:
from imaplib import IMAP4
c = IMAP4('newmailhost.cc.iitk.ac.in')
错误出现在 Web 服务器中,但不在本地主机外壳中..
socket.gaierror: [Errno -2] Name or service not known
在 mylib/backend.py 中
from django.contrib.auth.models import User
from imaplib import IMAP4
class MyCustomBackend:
# Create an authentication method
# This is called by the standard Django login procedure
def authenticate(self, username=None, password=None):
try:
# Check if this user is valid on the mail server
c = IMAP4('newmailhost.cc.iitk.ac.in')
c.login(username, password)
except:
return None
user, created = User.objects.get_or_create(username=username)
return user
# Required for your backend to work properly - unchanged in most scenarios
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
在setting.py中
AUTHENTICATION_BACKENDS = (
'mylib.backend.MyCustomBackend',
'django.contrib.auth.backends.ModelBackend',
)
【问题讨论】:
标签: python django sockets pythonanywhere