问题:Django服务器错误:端口已在使用中

重新启动Django服务器会显示以下错误:

this port is already running....

此问题专门在Ubuntu而不是其他操作系统上发生。如何释放端口以重新启动服务器?

Restarting the Django server displays the following error:

this port is already running....

This problem occurs specifically on Ubuntu and not other operating systems. How can I free up the port to restart the server?


回答 0

只需键入一个更简单的解决方案sudo fuser -k 8000/tcp。这将终止与端口8000相关的所有进程。

编辑:

对于osx用户,您可以使用 sudo lsof -t -i tcp:8000 | xargs kill -9

A more simple solution just type sudo fuser -k 8000/tcp. This should kill all the processes associated with port 8000.

EDIT:

For osx users you can use sudo lsof -t -i tcp:8000 | xargs kill -9


回答 1

netstat -ntlp

它将显示类似这样的内容。

   Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State           PID/Program name    
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      6599/python         
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      -                   
tcp        0      0 192.168.124.1:53        0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::3306                 :::*                    LISTEN     

因此,现在通过终止与Django / python相关的进程来关闭已经运行Django / python的端口。

kill -9 PID

就我而言

kill -9 6599

现在运行您的Django应用。

netstat -ntlp

It will show something like this.

   Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State           PID/Program name    
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      6599/python         
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      -                   
tcp        0      0 192.168.124.1:53        0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::3306                 :::*                    LISTEN     

So now just close the port in which Django/python running already by killing the process associated with it.

kill -9 PID

in my case

kill -9 6599

Now run your Django app.


回答 2

ps aux | grep -i manage

after that you will see all process 


ubuntu@ip-10-154-22-113:~/django-apps/projectname$ ps aux | grep -i manage
ubuntu    3439  0.0  2.3  40228 14064 pts/0    T    06:47   0:00 python manage.py runserver project name
ubuntu    3440  1.4  9.7 200996 59324 pts/0    Tl   06:47   2:52 /usr/bin/python manage.py runserver project name
ubuntu    4581  0.0  0.1   7988   892 pts/0    S+   10:02   0:00 grep --color=auto -i manage


kill -9 process id


e.d kill -9 3440


`enter code here`after that :

python manage.py runserver project name
ps aux | grep -i manage

after that you will see all process 


ubuntu@ip-10-154-22-113:~/django-apps/projectname$ ps aux | grep -i manage
ubuntu    3439  0.0  2.3  40228 14064 pts/0    T    06:47   0:00 python manage.py runserver project name
ubuntu    3440  1.4  9.7 200996 59324 pts/0    Tl   06:47   2:52 /usr/bin/python manage.py runserver project name
ubuntu    4581  0.0  0.1   7988   892 pts/0    S+   10:02   0:00 grep --color=auto -i manage


kill -9 process id


e.d kill -9 3440


`enter code here`after that :

python manage.py runserver project name

回答 3

缺省情况下,runserver命令在内部IP的端口8000上启动开发服务器。

如果要更改服务器的端口,请将其作为命令行参数传递。例如,此命令在端口8080上启动服务器:

python manage.py runserver 8080

By default, the runserver command starts the development server on the internal IP at port 8000.

If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:

python manage.py runserver 8080

回答 4

我们不使用此命令{sudo lsof -t -i tcp:8000 | xargs kill -9}因为它关闭了所有标签…您应该使用

ps -ef | grep python

杀死-9 process_id

ps -ef | grep python(显示所有具有id的进程)

kill -9 11633(11633是一个进程ID:-/ bin / python manage.py runserver)

We don’t use this command { sudo lsof -t -i tcp:8000 | xargs kill -9 } Because it’s close all tabs…You should use to

ps -ef | grep python

kill -9 process_id

ps -ef | grep python (show all process with id)

kill -9 11633 (11633 is a process id to :- /bin/python manage.py runserver)


回答 5

这是对Mounir的回答的扩展。我添加了一个bash脚本来为您解决这个问题。只是运行./scripts/runserver.sh而不是,./manage.py runserver它将以完全相同的方式工作。

#!/bin/bash

pid=$(ps aux | grep "./manage.py runserver" | grep -v grep | head -1 | xargs | cut -f2 -d" ")

if [[ -n "$pid" ]]; then
    kill $pid
fi

fuser -k 8000/tcp
./manage.py runserver

This is an expansion on Mounir’s answer. I’ve added a bash script that covers this for you. Just run ./scripts/runserver.sh instead of ./manage.py runserver and it’ll work exactly the same way.

#!/bin/bash

pid=$(ps aux | grep "./manage.py runserver" | grep -v grep | head -1 | xargs | cut -f2 -d" ")

if [[ -n "$pid" ]]; then
    kill $pid
fi

fuser -k 8000/tcp
./manage.py runserver

回答 6

抱歉在旧帖子中发表评论,但这可能会对人们有所帮助

只需在您的终端上输入

killall -9 python3

它将杀死计算机上运行的所有python3,并释放所有端口。何时在Django项目中工作对我有很大帮助。

Sorry for comment in an old post but It may help people

Just type this on your terminal

killall -9 python3

It will kill all python3 running on your machine and it will free your all port. Greatly help me when to work in Django project.


回答 7

对我来说,发生这种情况是因为我的应用程序中的调试器断点拦截了Postman中的API请求,从而使请求挂起。如果我在终止我的应用程序服务器之前在Postman中取消了该请求,则该错误不会首先发生。

->因此,请尝试取消您在其他程序中发出的所有打开的请求。

在macOS上,我一直sudo lsof -t -i tcp:8000 | xargs kill -9在忘记取消打开的http请求来解决error = That port is already in use.该问题,这也完全关闭了我的Postman应用程序,这就是为什么我的第一个解决方案更好的原因。

For me, this happens because my API request in Postman is being intercepted by a debugger breakpoint in my app… leaving the request hanging. If I cancel the request in Postman before killing my app’s server, the error does not happen in the first place.

–> So try cancelling any open requests you are making in other programs.

On macOS, I have been using sudo lsof -t -i tcp:8000 | xargs kill -9 when I forget to cancel the open http request in order to solve error = That port is already in use. This also, complete closes my Postman app, which is why my first solution is better.


回答 8

在该ctl-c之后键入’fg’作为命令。
命令:
Fg将显示哪个正在后台运行。之后,ctl-c将停止它。

fg
ctl-c

Type ‘fg’ as command after that ctl-c.
Command:
Fg will show which is running on background. After that ctl-c will stop it.

fg
ctl-c


回答 9

ps aux | grep管理

ubuntu 3438 127.0.0 2.3 40256 14064 pts / 0 T 06:47 0:00 python manage.py runserver

杀死-9 3438

ps aux | grep manage

ubuntu 3438 127.0.0 2.3 40256 14064 pts/0 T 06:47 0:00 python manage.py runserver

kill -9 3438


回答 10

似乎是IDE,VSCode,Puppeteer,nodemon,express等引起了此问题,您在后台运行了一个进程,或者只是关闭了调试区域[浏览器,终端等]或其他任何东西,无论如何,我都回答了​​同样的问题以前,这是您的链接

https://stackoverflow.com/a/49797588/2918720

It seems that IDEs, VSCode, Puppeteer, nodemon, express, etc. causes this problem, you ran a process in the background or just closed the debugging area [browser, terminal, etc. ] or whatever , anyway, i have answered same question before, Here you are it’s link

https://stackoverflow.com/a/49797588/2918720


回答 11

如果您在Mac中遇到此问题,则只需打开活动监视器并强制使用python,然后重试

在此处输入图片说明

if you have face this problem in mac you just need to open activity monitor and force quite python then try again

enter image description here


回答 12

lsof -t -i tcp:8000 | xargs杀死-9

lsof -t -i tcp:8000 | xargs kill -9


回答 13

如果您使用的是VSC的屏幕终端,则该错误可能是由于您已经在其他Shell中运行了服务器。

只需单击VSC终端标题中+号左侧的下拉框,然后选择其他外壳程序,然后检查服务器是否已在此处运行。退出该服务器,您就可以启动另一个服务器了。

In case You are using the VSC’s screen terminal, The error might be due to the fact that you already runserver in some other shell.

Just click on the dropbox on the left of the + sign in the header of the terminal of VSC and select some other shell and check if the server is already running there. Quit that server and you are ready to launch a another server.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。