Python Django 服务器错误:端口已被使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20239232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django Server Error: port is already in use
提问by Ashish Kumar Saxena
Restarting the Django server displays the following error:
重新启动 Django 服务器显示以下错误:
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?
此问题专门发生在 Ubuntu 上,而不是其他操作系统上。如何释放端口以重新启动服务器?
采纳答案by Mounir
A more simple solution just type sudo fuser -k 8000/tcp.
This should kill all the processes associated with port 8000.
一个更简单的解决方案只需输入sudo fuser -k 8000/tcp. 这应该会杀死与端口 8000 相关的所有进程。
EDIT:
编辑:
For osx users you can use sudo lsof -t -i tcp:8000 | xargs kill -9
对于 osx 用户,您可以使用 sudo lsof -t -i tcp:8000 | xargs kill -9
回答by Ashish Kumar Saxena
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
回答by jstaab
This is an expansion on Mounir's answer. I've added a bash script that covers this for you. Just run ./scripts/runserver.shinstead of ./manage.py runserverand it'll work exactly the same way.
这是对穆尼尔回答的扩展。我已经为你添加了一个 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
回答by Shekhar Singh Choudhary
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.
所以现在只需通过终止与之关联的进程来关闭 Django/python 已经运行的端口。
kill -9 PID
in my case
就我而言
kill -9 6599
Now run your Django app.
现在运行你的 Django 应用程序。
回答by Mr Singh
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
我们不使用这个命令 { sudo lsof -t -i tcp:8000 | xargs kill -9 } 因为它关闭了所有选项卡......你应该使用
ps -ef | grep python
kill -9 process_id
ps -ef | 蟒蛇
kill -9 process_id
ps -ef | grep python (show all process with id)
ps -ef | grep python(显示所有进程id)
kill -9 11633 (11633 is a process id to :- /bin/python manage.py runserver)
kill -9 11633(11633 是一个进程 ID 到 :- /bin/python manage.py runserver)
回答by Basant Rules
ps aux | grep manage
ps辅助| grep 管理
ubuntu 3438 127.0.0 2.3 40256 14064 pts/0 T 06:47 0:00 python manage.py runserver
ubuntu 3438 127.0.0 2.3 40256 14064 pts/0 T 06:47 0:00 python manage.py runserver
kill -9 3438
杀死 -9 3438
回答by KhogaEslam
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
似乎是 IDE、VSCode、Puppeteer、nodemon、express 等导致了这个问题,你在后台运行了一个进程或者只是关闭了调试区域 [浏览器、终端等] 或其他什么,无论如何,我已经回答了同样的问题之前,这是你的链接
回答by Freddy
By default, the runserver command starts the development server on the internal IP at port 8000.
默认情况下,runserver 命令在端口 8000 的内部 IP 上启动开发服务器。
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:
如果要更改服务器的端口,请将其作为命令行参数传递。例如,此命令在端口 8080 上启动服务器:
python manage.py runserver 8080
回答by HashRocketSyntax
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.
对我来说,发生这种情况是因为我在 Postman 中的 API 请求被我的应用程序中的调试器断点拦截了......使请求挂起。如果我在杀死我的应用程序的服务器之前取消 Postman 中的请求,则首先不会发生错误。
--> 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 -9when 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.
在 macOS 上,我一直在使用sudo lsof -t -i tcp:8000 | xargs kill -9当我忘记取消打开的 http 请求以解决error = That port is already in use.此问题时,也完全关闭了我的 Postman 应用程序,这就是为什么我的第一个解决方案更好的原因。
回答by mahbubcseju
Type 'fg' as command after that ctl-c.
Command:
Fg will show which is running on background. After that ctl-c will stop it.
在 ctl-c 之后输入“fg”作为命令。
命令:
Fg 将显示哪个在后台运行。之后 ctl-c 将停止它。
fg
ctl-c
fg
ctl-c

