curl: (7) 连接 127.0.0.1 端口 8000 失败:连接被拒绝

Posted

技术标签:

【中文标题】curl: (7) 连接 127.0.0.1 端口 8000 失败:连接被拒绝【英文标题】:curl: (7) Failed to connect to 127.0.0.1 port 8000: Connection refused 【发布时间】:2021-01-27 10:28:58 【问题描述】:

我正在尝试使用 Django 和 Vue.js 制作聊天应用程序。为此,我开始创建一个 Django 项目,在其中使用 pip 安装了 Django REST 框架和 djoser,并在 urls.py 中添加了相应的 url。

但是当我尝试使用 [curl -X POST http://127.0.0.1:8000/auth/users/create/ --data] 创建用户时,我得到的是 "curl: ( 7) 无法连接到 127.0.0.1 端口 8000:连接被拒绝”。以下是所需的脚本。

urls.py:

    """chatire URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('auth/', include('djoser.urls')),
    path('auth/', include('djoser.urls.authtoken')),
]

settings.py:

"""
Django settings for chatire project.

Generated by 'django-admin startproject' using Django 3.1.2.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'dns*bsa6_2)bbdglt)jj=o-912h6=lyo#7qshl7&htuys%400&'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'djoser',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'chatire.urls'

TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': 
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        ,
    ,
]

WSGI_APPLICATION = 'chatire.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    



# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    ,
    
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    ,
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

REST_FRAMEWORK = 
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    ),

【问题讨论】:

你如何运行你的项目? Connection Refused 错误表明没有进程正在侦听该端口。 使用下面的命令我正在运行项目-python manage.py runserver,我运行它时没有收到错误,但是当我运行它时----> curl -X POST @ 987654321@ --data ,我收到了这个错误 你确定 Django 正在监听那个端口吗?如果您使用的是 Linux,请尝试 sudo netstat -ntlp 并查看您的应用使用的端口。 没有端口不在监听。我已经使用 netstat -a 进行了检查。顺便说一句,我正在使用Windows。我在项目中使用 8000 端口,但是当使用 netstat 命令检查时,我无法找到 8000 端口监听。 嗯,你是使用 WSL 来运行 django 还是 curl? 【参考方案1】:

打开 settings.py 文件并进行这些更改

DEBUG = False
 
ALLOWED_HOSTS = ['*']

【讨论】:

以上是关于curl: (7) 连接 127.0.0.1 端口 8000 失败:连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

PHP CURL本地URL时,一直处于 pending 状态

Laravel Valet 共享 - 无法连接到 127.0.0.1 端口 4040:连接被拒绝

服务器是不是在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接?

curl: Failed to connect to 127.0.0.1 port 1086: Connection refused

该设备或资源(127.0.0.1)未设置为接受端口“16823”上的连接。

我遇到的错误curl: Failed to connect to 127.0.0.1 port 1086: Connection refused