Celery has DNS resolution problems? - python

I used Celery for email registration asynchronously, but when I trigger this asynchronous request, the following error will appear
Traceback (most recent call last):
File "d:\python3\lib\site-packages\dns\resolver.py", line 982, in nameservers
raise NotImplementedError
NotImplementedError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\python3\lib\site-packages\celery\app\trace.py", line 451, in trace_task
R = retval = fun(*args, **kwargs)
File "d:\python3\lib\site-packages\celery\app\trace.py", line 734, in __protected_call__
return self.run(*args, **kwargs)
File "E:\Exploit_blog\blog_index_html\tasks.py", line 9, in send_email
send_mail(
File "d:\python3\lib\site-packages\django\core\mail\__init__.py", line 61, in send_mail
return mail.send()
File "d:\python3\lib\site-packages\django\core\mail\message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "d:\python3\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "d:\python3\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "d:\python3\lib\smtplib.py", line 1034, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "d:\python3\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "d:\python3\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "d:\python3\lib\smtplib.py", line 1040, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "d:\python3\lib\smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "d:\python3\lib\site-packages\eventlet\green\socket.py", line 44, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 543, in getaddrinfo
qname, addrs = _getaddrinfo_lookup(host, family, flags)
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 505, in _getaddrinfo_lookup
answer = resolve(host, qfamily, False, use_network=use_network)
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 450, in resolve
return _proxy.query(name, rdtype, raise_on_no_answer=raises,
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 394, in query
step(self._resolver.query, qname, rdtype, rdclass, tcp, source, raise_on_no_answer=False)
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 334, in _resolver
self.clear()
File "d:\python3\lib\site-packages\eventlet\support\greendns.py", line 342, in clear
self._resolver = dns.resolver.Resolver(filename=self._filename)
File "d:\python3\lib\site-packages\dns\resolver.py", line 756, in __init__
self.read_registry()
File "d:\python3\lib\site-packages\dns\resolver.py", line 858, in read_registry
self.nameservers = info.nameservers
File "d:\python3\lib\site-packages\dns\resolver.py", line 984, in nameservers
raise ValueError(f'nameserver {nameserver} is not an '
ValueError: nameserver ; is not an IP address or valid https URL
There is no problem with the celery tasks code, because I was able to use it before, but the above situation appeared after I changed a computer
tasks code
from django.core.mail import send_mail
from celery import shared_task
# 发送邮件验证码,使用task进行异步处理,获取从视图层获取来的值,然后发送到用户邮箱中
#shared_task
def send_email(request, rand_str):
send_mail(
subject='验证码',
message='你的验证码为:' + rand_str + '请不要将你的验证码告诉其他人',
from_email='TEXT',
recipient_list=[request],
fail_silently=False
)
return (request, rand_str)
main settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'xxx'
EMAIL_HOST_PASSWORD = 'xxx'
.> celery exchange=celery(direct) key=celery
Celery startup console can recognize this tasks
[tasks]
. Let.tasks.add
. blog_index_html.tasks.send_email

When I searched for a few days, I determined the BUG location, it turned out that the eventlet and dnspython modules were incompatible, and I found a solution in his official GIT repository.
My operation is to uninstall and reinstall the dnspython module
github link

Related

Python gmail imap/pop issues

I'm trying to write code to view messages from my g-mail, then search in the latest message for a certain phrase, that triggers a command.
But I'm having problems with connecting to my g-mail
from imapclient import IMAPClient
HOST = 'imap.gmail.com'
USERNAME = 'email'
PASSWORD = 'password'
ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
server.select_folder('INBOX')
print ('success')
I just added the 'success' at the end to make sure it worked, I'm using imap here but I tried pop which seems to be more confusing.
When I run this code i get this.
Traceback (most recent call last):
File "C:/Users/Yousef/Desktop/parser.py", line 8, in <module>
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imapclient.py", line 152, in __init__
self._imap = self._create_IMAP4()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imapclient.py", line 166, in _create_IMAP4
return imap4.IMAP4WithTimeout(self.host, self.port, self._timeout)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imap4.py", line 13, in __init__
imaplib.IMAP4.__init__(self, address, port)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\imaplib.py", line 197, in __init__
self.open(host, port)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\imaplib.py", line 294, in open
self.sock = self._create_socket()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imap4.py", line 16, in _create_socket
return socket.create_connection((self.host, self.port), self._timeout)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\socket.py", line 722, in create_connection
raise err
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\socket.py", line 713, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond
Someone please help me with this, because this is my first time using imap/pop.
when I put ssl to true, I get this...
Traceback (most recent call last):
File "C:\Users\Yousef\Desktop\parser.py", line 8, in <module>
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imapclient.py", line 152, in __init__
self._imap = self._create_IMAP4()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\imapclient.py", line 164, in _create_IMAP4
self._timeout)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\tls.py", line 171, in __init__
imaplib.IMAP4.__init__(self, host, port)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-
32\lib\imaplib.py", line 197, in __init__
self.open(host, port)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\tls.py", line 177, in open
self.sock = wrap_socket(sock, self.ssl_context, host)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\tls.py", line 144, in wrap_socket
ssl_context = create_default_context()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\imapclient\tls.py", line 127, in create_default_context
context.load_verify_locations(cadata=certs)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\backports\ssl\core.py", line 654, in load_verify_locations
self._ctx.load_verify_locations(cafile, capath)
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\OpenSSL\SSL.py", line 669, in load_verify_locations
_raise_current_error()
File "C:\Users\Yousef\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\OpenSSL\_util.py", line 54, in exception_from_error_queue
raise exception_type(errors)
OpenSSL.SSL.Error: []
nvm i found a solution, thanks for the help anyways.

Django-registration "gaierror at /accounts/register/ [Errno -2] Name or service not known"

I am trying to implement django-registration, but I get a gaierror on the registration page with the following traceback:
Internal Server Error: /accounts/register/
Traceback (most recent call last):
#irlevant
File "/usr/local/lib/python2.7/dist-packages/registration/views.py", line 36, in form_valid
new_user = self.register(form)
File "/usr/local/lib/python2.7/dist-packages/registration/backends/model_activation/views.py", line 29, in register
site=get_current_site(self.request)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py", line 184, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/registration/models.py", line 97, in create_inactive_user
registration_profile.send_activation_email(site)
File "/usr/local/lib/python2.7/dist-packages/registration/models.py", line 182, in send_activation_email
self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 363, in email_user
send_mail(subject, message, from_email, [self.email], **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
return mail.send()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 292, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno -2] Name or service not known
Here is my settings.py:
DEBUG = True
ALLOWED_HOSTS = ['*']
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '********#gmail.com'
EMAIL_HOST_PASSWORD = '*******'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
ACCOUNT_ACTIVATION_DAYS = 1
REGISTRATION_AUTO_LOGIN = True
SITE_ID = 1
LOGIN_REDIRECT_URL = '/'
Where should i check to find the mistake?
I faces the same problem, it was just an unstable internet connection because the problem disappeared as I changed my connection.

Issue with setting up email sender with Django 1.8 and Plesk

I'm trying to configure a website using Django 1.8 and Plesk, and setting the email variables to any value gets the traceback below. I've tried with Gmail and the email server from the host, and the error is the same. I have tested Python's smtplib and successfully sent an email through that, so it looks like it may be something between Django and Plesk. If I set up the website on a test CentOS 6 server, then I don't run into this problem.
Example Config settings
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_PASSWORD = "mypassword"
EMAIL_HOST_USER = "user#gmail.com"
EMAIL_PORT = 587
Traceback
mod_wsgi (pid=1703): Exception occurred processing WSGI script '/var/www/vhosts/s1085163.instanturl.net/oozoa.wsgi'.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/whitenoise/base.py", line 119, in __call__
return self.application(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 256, in handle_uncaught_exception
'request': request
File "/usr/local/lib/python2.7/logging/__init__.py", line 1185, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/local/lib/python2.7/logging/__init__.py", line 1278, in _log
self.handle(record)
File "/usr/local/lib/python2.7/logging/__init__.py", line 1288, in handle
self.callHandlers(record)
File "/usr/local/lib/python2.7/logging/__init__.py", line 1328, in callHandlers
hdlr.handle(record)
File "/usr/local/lib/python2.7/logging/__init__.py", line 751, in handle
self.emit(record)
File "/usr/local/lib/python2.7/site-packages/django/utils/log.py", line 129, in emit
self.send_mail(subject, message, fail_silently=True, html_message=html_message)
File "/usr/local/lib/python2.7/site-packages/django/utils/log.py", line 132, in send_mail
mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 98, in mail_admins
mail.send(fail_silently=fail_silently)
File "/usr/local/lib/python2.7/site-packages/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/local/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/local/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno -2] Name or service not known
Might be there is an issues with the firewall. Please try to disabled plesk firewall and IPtables on your server for the testing purpose and check it again.

gaierror while using send_mail in django behind proxy

I was trying to send an email from my Django app using gmail. I added these email settings to settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
But it throws this name resolution error:
Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.4/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.4/smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.4/smtplib.py", line 292, in _get_socket
self.source_address)
File "/usr/lib/python3.4/socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
Note: I am working behind a proxy.
In addition, I tried to connect to google.com through socket:
import socket
socket.gethostbyname('www.google.com')
It throws:
[Errno 11001] getaddrinfo failed
enable pop3 in gmail.com
create specific password for django application
(http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833)
Solutions here

Django send_mail and Exchange server

Currently I have an Exchange server running and am attempting to run send_mail, though get a variety of errors.
Here is my current settings.py
EMAIL_HOST = 'exchange.domain.com'
EMAIL_HOST_USER = 'DOMAIN\user'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 443
EMAIL_USE_TLS = True
and when I run send_mail('''Subject''', '''Message.''', 'myemail#domain.com', ['otheremail#domain.com',], fail_silently=False)
after a very long wait
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
connection=connection).send()
File "/Library/Python/2.7/site-packages/django/core/mail/message.py", line 255, in send
return self.get_connection(fail_silently).send_messages([self])
File "/Library/Python/2.7/site-packages/django/core/mail/backends/smtp.py", line 88, in send_messages
new_conn_created = self.open()
File "/Library/Python/2.7/site-packages/django/core/mail/backends/smtp.py", line 49, in open
local_hostname=DNS_NAME.get_fqdn())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 250, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 311, in connect
(code, msg) = self.getreply()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 359, in getreply
+ str(e))
SMTPServerDisconnected: Connection unexpectedly closed: [Errno 54] Connection reset by peer

Categories