IMAP4在pythonanywhere服务器上不工作,但在localhos上工作的代码相同

2024-10-03 17:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用户登录我的django网络应用使用我们的研究所邮件服务器。 但这段代码允许在localhost上使用协会邮件登录,而不是在服务器上。你知道吗

我试过Python,django提供贝壳:

from imaplib import IMAP4
c = IMAP4('newmailhost.cc.iitk.ac.in')

错误出现在web服务器中,但不在本地主机shell中。。你知道吗

socket.gaierror: [Errno -2] Name or service not known

在mylib/后端.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

在设置.py你知道吗

AUTHENTICATION_BACKENDS = (
    'mylib.backend.MyCustomBackend',
    'django.contrib.auth.backends.ModelBackend',

)

Tags: djangoinfromimport服务器nonegetreturn
1条回答
网友
1楼 · 发布于 2024-10-03 17:24:51

这个DNS名称不能解析为我所看到的地址。所以我猜这是一个在你的网络之外不存在的私有服务器。你知道吗

相关问题 更多 >