【问题标题】:IMAP4 not working on python anywhere server but same code works on localhostIMAP4 无法在任何地方的 python 服务器上运行,但相同的代码在 localhost 上运行
【发布时间】: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


    【解决方案1】:

    该 DNS 名称无法解析为我所看到的地址。所以我的猜测是它是一个不存在于您的网络之外的私有服务器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 2012-07-06
      • 2019-05-21
      • 2020-11-25
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多