简体   繁体   English

Pythonanywhere - ConnectionRefusedError: [Errno 111]

[英]Pythonanywhere - ConnectionRefusedError: [Errno 111]

Here I have a model called User.在这里,我有一个名为 User 的模型。 It has 2 fields "Name" and "Phone".它有 2 个字段“姓名”和“电话”。 I am trying to authenticate with an OTP.我正在尝试使用 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出于这个原因,我在 Pythonanywhere 上托管了该项目,但现在它在服务器上出现错误ConnectionRefusedError: [Errno 111] Connection denied

Essentials必需品

Below is the code for the OTP message to send下面是要发送的 OTP 消息的代码

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

View看法

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)

Error错误

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

Postman response邮递员回复

<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.您可能使用的是需要通过我们的代理访问互联网的免费帐户,但我没有看到您已将 http 库配置为使用代理的任何地方。 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.在 PythonAnywhere 帮助页面中搜索“代理”以查找代理详细信息并配置您的 http 库以使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM