Getting " ConnectionAbortedError: [ WinError 10053] " while using Django Email in a POST request

397 views
Skip to first unread message

Jose Chacón

unread,
Jan 16, 2020, 3:54:17 PM1/16/20
to Django users
Hello guys , i've came across to this error while using django email in a method which is fired after a post request to register new users . The goal of this method is to after a user is created,  an email must be sent to user's mail in order to finish registration workflow. Every time the msg.send() is executed  the following message gets printed in the terminal

[16/Jan/2020 11:15:28] "POST /api/v1/auth/usuario/ HTTP/1.1" 400 54
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 58382)
Traceback (most recent call last):
  File "c:\users\consultor2015\appdata\local\programs\python\python38-32\Lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "c:\users\consultor2015\appdata\local\programs\python\python38-32\Lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\users\consultor2015\appdata\local\programs\python\python38-32\Lib\socketserver.py", line 720, in __init__
    self.handle()
  File "D:\JITZONE\JitZone\venv\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "D:\JITZONE\JitZone\venv\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "c:\users\consultor2015\appdata\local\programs\python\python38-32\Lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

Although doesn't affect functionality (email still get sent) , it's bugging me  because this error makes the POST request canceled .
Here are my methods:


1.views.py 

class ProfileCreateAPIView(CreateAPIView):
queryset = User.objects.all()
model = UsuarioPerfil
serializer_class = UserSerializer

2.serializers.py


class UserSerializer(serializers.ModelSerializer):
usuario_perfil = UsuarioPerfilSerializer()
generar_correo_confirmacion = serializers.CharField(required=False)
enviar_correo = serializers.CharField(required=False)

class Meta:
model = User
fields = ['password', 'email', 'username', 'first_name', 'last_name', 'usuario_perfil',
'generar_correo_confirmacion', 'enviar_correo']

def create(self, validated_data):

#######################CREATION OF USER AND USERPROFILE#####################################
        profile_data = validated_data.pop('usuario_perfil')
user = User.objects.create(
username=validated_data['username'],
email=validated_data['email'],
first_name=validated_data["first_name"],
last_name=validated_data["last_name"],
is_active=False
)
user.set_password(validated_data['password'])
user.save()
UsuarioPerfil.objects.create(
usuario=user,
perfil=perfil,
red_social=red_social,
**profile_data)

###########CONFIRMATION EMAIL###############################################################
token = account_validation_token.make_token(user)
user_id = urlsafe_base64_encode(force_bytes(user.id))
url = 'http://localhost:8000' + reverse('usuarios:confirmacionReinicioPassword',
kwargs={'user_id': user_
id, 'token': token})

try:
htmly = get_template(
'mail/recuperacion_contrasenia.html')
d = {
'confirm_url': url}
subject, from_email, to =
'Confirmacion de cuenta', settings.EMAIL_HOST_USER, user.email
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
msg.attach_alternative(html_content,
"text / html")
msg.send()
except:
print(":c")

return user


What it's confusing me its that there's no trace of this error if i send an email in a View which gets fired after a GET request.  Also i've tried putting the logic in a post method in django view , without using Django Rest Framework,  but the error still appears 

I'd be so grateful if you help me. Thanks in advance 
José Chacón


Nosa Omorodion

unread,
Jan 16, 2020, 4:11:07 PM1/16/20
to django...@googlegroups.com
Have you tried sending this mail from the user model? 

Using the post save signal 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d1cdb3b2-0f25-4ce5-b66e-3bbdb66e0cac%40googlegroups.com.

maninder singh Kumar

unread,
Jan 16, 2020, 10:59:57 PM1/16/20
to django...@googlegroups.com
Perhaps a problem with the smtp server, I am wondering if the email object got initialized at all.
 
               
 


Mike Dewhirst

unread,
Jan 17, 2020, 2:14:29 AM1/17/20
to django...@googlegroups.com
I get exactly those errors all the time. I believe it is related to
Windows being Windows. It appears to have nothing to do with any code I
have written. Everything still works.

It looks like an unknown glitch occurs and the current request gets
finished (handle_request in socket_server.py) gracefully and then the
thread gets terminated. A new one seems to get created automatically by
the dev server for the next request.

Stopping it is way above my pay grade.

I have long since stopped worrying about it.

It's lucky that Tim Berners-Lee made the web stateless hey?

Cheers

Mike

On 17/01/2020 2:58 pm, maninder singh Kumar wrote:
> Perhaps a problem with the smtp server, I am wondering if the email
> object got initialized at all.
> --
>
> Maninder Kumar
> http://about.me/maninder.s.kumar
>
> <http://about.me/maninder.s.kumar?promo=email_sig>
> **
>
> *1.views.py <http://1.views.py> *
>
> class ProfileCreateAPIView(CreateAPIView):
> queryset = User.objects.all()
> model = UsuarioPerfil
> serializer_class = UserSerializer
>
> *2.serializers.py <http://2.serializers.py>*
>
> *
> *
>
> class UserSerializer(serializers.ModelSerializer):
> usuario_perfil = UsuarioPerfilSerializer()
> generar_correo_confirmacion =
> serializers.CharField(required=False) enviar_correo =
> serializers.CharField(required=False) class Meta: model = User
> fields = ['password', 'email', 'username', 'first_name',
> 'last_name', 'usuario_perfil', 'generar_correo_confirmacion',
> 'enviar_correo'] def create(self, validated_data):
> #######################CREATION OF USER AND
> USERPROFILE#####################################
>
> profile_data = validated_data.pop('usuario_perfil') user =
> User.objects.create( username=validated_data['username'],
> email=validated_data['email'],
> first_name=validated_data["first_name"],
> last_name=validated_data["last_name"], is_active=False )
> user.set_password(validated_data['password']) user.save()
> UsuarioPerfil.objects.create( usuario=user, perfil=perfil,
> red_social=red_social, **profile_data) ###########CONFIRMATION
> EMAIL###############################################################
> token = account_validation_token.make_token(user) user_id =
> urlsafe_base64_encode(force_bytes(user.id <http://user.id>))
> url = 'http://localhost:8000' +
> reverse('usuarios:confirmacionReinicioPassword',
> kwargs={'user_id': user_*id*, 'token': token}) try: htmly =
> get_template('mail/recuperacion_contrasenia.html') d =
> {'confirm_url': url} subject, from_email, to = 'Confirmacion
> de cuenta', settings.EMAIL_HOST_USER, user.email html_content
> = htmly.render(d) msg = EmailMultiAlternatives(subject,
> html_content, from_email, [to])
> msg.attach_alternative(html_content, "text / html") msg.send()
> except: print(":c") return user
>
>
> What it's confusing me its that there's no trace of this error
> if i send an email in a View which gets fired after a GET
> request.  Also i've tried putting the logic in a post method
> in django view , without using Django Rest Framework,  but the
> error still appears
>
> I'd be so grateful if you help me. Thanks in advance
> José Chacón
>
>
> --
> You received this message because you are subscribed to the
> Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/d1cdb3b2-0f25-4ce5-b66e-3bbdb66e0cac%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMDZ7Cw612WQOjEPynhBZiXj5DJ-XsfCC-7GL_8qGL_CSxAHWQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAMDZ7Cw612WQOjEPynhBZiXj5DJ-XsfCC-7GL_8qGL_CSxAHWQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CABOHK3SuuOyhgqiP4de1H%3DMSWZ7dHLAkRSG7Yvhs1q47JAp%2BoA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CABOHK3SuuOyhgqiP4de1H%3DMSWZ7dHLAkRSG7Yvhs1q47JAp%2BoA%40mail.gmail.com?utm_medium=email&utm_source=footer>.

maninder singh Kumar

unread,
Jan 17, 2020, 2:47:02 AM1/17/20
to django...@googlegroups.com
Here's the code that I use :

with mail.get_connection() as connection:
        connection.open()
send_mail(Subject ,Message, Email, recipient_list, connection=connection)
        connection.close()
Works beautifully, it is available at django docs

 
               
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d1cdb3b2-0f25-4ce5-b66e-3bbdb66e0cac%40googlegroups.com.

Kasper Laudrup

unread,
Jan 17, 2020, 3:13:02 AM1/17/20
to django...@googlegroups.com
Hi Maninder,

On 17/01/2020 08.46, maninder singh Kumar wrote:
> Here's the code that I use :
>
> with mail.get_connection() as connection:
>         connection.open()
> send_mail(Subject ,Message, Email, recipient_list, connection=connection)
>         connection.close()
> Works beautifully, it is available at django docs
>

Just out of curiosity, isn't the call to connection.close() redundant if
you already use a context manager for the connection?

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages