Pythonanywhere - ConnectionRefusedError: [Errno 111]

Pythonanywhere - ConnectionRefusedError: [Errno 111]

这里我有一个名为 User 的模型。它有 2 个字段 "Name" 和 "Phone"。 我正在尝试使用 OTP 进行身份验证。实际上代码在本地服务器上运行良好。为此,我将项目托管在 Pythonanywhere 上,但现在在服务器上出现错误 ConnectionRefusedError: [Errno 111] 连接被拒绝

要点

下面是发送 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

查看

这是我的看法

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)

错误

这是服务器错误

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>

有人可以帮我解决这个问题吗?

您可能使用的是免费帐户,需要通过我们的代理访问互联网,但我在任何地方都看不到您已将 http 库配置为使用代理。某些库(如请求)会自动使用代理设置,而其他库则不会。在 PythonAnywhere 帮助页面中搜索 "proxy" 以查找代理详细信息并配置您的 http 库以使用它们。