Here I have a model called User. It has 2 fields "Name" and "Phone". I am trying to authenticate with an OTP. Actually the code works fine on the local server. For this reason, I hosted the project on Pythonanywhere, but now it's getting an error on the server ConnectionRefusedError: [Errno 111] Connection refused
Below is the code for the OTP message to send
def generate_otp(user, name, phone):
otp = users.models.User.objects.make_random_password(length=4, allowed_chars='123456789')
if not users.models.VerifyToken.objects.filter(user=user).exists():
users.models.VerifyToken.objects.create(user=user, otp=otp, name=name, phone=phone)
send_verification_message(phone, otp)
else:
token = users.models.VerifyToken.objects.get(user=user)
token.otp = otp
token.name = name
token.phone = phone
send_verification_message(phone, otp)
token.save()
return otp
def send_verification_message(phone, otp):
phone = phone[3:14]
conn = http.client.HTTPSConnection("api.msg91.com")
payload = "{ \"sender\": \"BMyDay\", \"route\": \"4\", \"country\": \"91\", \"sms\": [ { \"message\": \"" + "Your verification code is " + otp + ".\", \"to\": [ \"" + phone + "\" ] } ] }"
print(payload)
headers = {
'authkey': "xxxxxxxxxxxxxxxxxxx",
'content-type': "application/json"
}
conn.request("POST", "/api/v2/sendsms", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
return True
This is my view
class AuthAPIView(APIView):
queryset = User.objects.all()
lookup_field = "phone"
permission_classes = [AllowAny]
# Get OTP
def put(self, request):
name = request.data.get('name')
phone = request.data.get('phone')
if name and phone and phonenumbers.is_valid_number(
phonenumbers.parse(request.data.get('phone'), 'IN')):
# if user is exists
if VerifyToken.objects.filter(phone=phone).exists():
user = VerifyToken.objects.get(phone=phone).user
generate_otp(user, name, phone)
elif User.objects.filter(phone=phone).exists():
user = User.objects.get(phone=phone)
generate_otp(user, name, phone)
else:
username = User.objects.make_random_password(length=10)
user = User.objects.create_user(username=username, phone=phone)
generate_otp(user, name, phone)
return Response({'response': 'OTP send to your mobile number: ' + str(phone), "user": user.id},
status=status.HTTP_200_OK)
else:
return Response({'response': 'Enter a valid data'}, status=status.HTTP_400_BAD_REQUEST)
This is the server error
Traceback (most recent call last):
File "/home/BookMyDay/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/BookMyDay/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/BookMyDay/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/BookMyDay/.local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/BookMyDay/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/BookMyDay/.local/lib/python3.6/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/home/BookMyDay/.local/lib/python3.6/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/BookMyDay/.local/lib/python3.6/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/home/BookMyDay/.local/lib/python3.6/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/home/BookMyDay/bookmyday_python/source/users/api_view.py", line 121, in put
generate_otp(user, name, phone)
File "/home/BookMyDay/bookmyday_python/source/source/essentials.py", line 42, in generate_otp
send_verification_message(phone, otp)
File "/home/BookMyDay/bookmyday_python/source/source/essentials.py", line 63, in send_verification_message
conn.request("POST", "/api/v2/sendsms", payload, headers)
File "/usr/lib/python3.6/http/client.py", line 1254, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1300, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1249, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1036, in _send_output
self.send(msg)
File "/usr/lib/python3.6/http/client.py", line 974, in send
self.connect()
File "/usr/lib/python3.6/http/client.py", line 1407, in connect
super().connect()
File "/usr/lib/python3.6/http/client.py", line 946, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
<h1>Server Error (500)</h1>
Can someone help me with this?
You're probably on a free account that needs to access the internet through our proxy, but I don't see anywhere that you have configured your http library to use the proxy. Some libraries (like requests) use the proxy settings automatically and others do not. Search the PythonAnywhere help pages for "proxy" to find the proxy details and configure your http library to use them.