IMAP4 无法在 python 任何服务器上运行,但相同的代码可在本地主机上运行
IMAP4 not working on python anywhere server but same code works on localhost
我想让用户使用我们学院的邮件服务器登录我的 django 网络应用程序。
但是此代码允许在本地主机上使用学院邮件登录,但不能在服务器上登录。
我试过 django 提供的 pythonanywhere shell:
from imaplib import IMAP4
c = IMAP4('newmailhost.cc.iitk.ac.in')
错误出现在 Web 服务器中,但不出现在本地主机中 shell..
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',
)
据我所知,该 DNS 名称无法解析为地址。所以我的猜测是它是一个不存在于您的网络之外的私人服务器。
我想让用户使用我们学院的邮件服务器登录我的 django 网络应用程序。 但是此代码允许在本地主机上使用学院邮件登录,但不能在服务器上登录。
我试过 django 提供的 pythonanywhere shell:
from imaplib import IMAP4
c = IMAP4('newmailhost.cc.iitk.ac.in')
错误出现在 Web 服务器中,但不出现在本地主机中 shell..
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',
)
据我所知,该 DNS 名称无法解析为地址。所以我的猜测是它是一个不存在于您的网络之外的私人服务器。