Python [Errno 98] 地址已被使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4465959/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:52:11  来源:igfitidea点击:

Python [Errno 98] Address already in use

pythonsocketsconnectionerrno

提问by skylerl

In my Python socket program, I sometimes need to interrupt it with Ctrl-C. When I do this, it does close the connection using socket.close().

在我的 Python 套接字程序中,我有时需要用Ctrl-C. 当我这样做时,它确实使用socket.close().

However, when I try to reopen it I have to wait what seems like a minute before I can connect again. How does one correctly close a socket? Or is this intended?

但是,当我尝试重新打开它时,我必须等待大约一分钟才能再次连接。如何正确关闭套接字?或者这是有意为之?

采纳答案by Bartosz

Yes, it is intended. Here you can read detailed explanation. It is possible to override this behavior by setting SO_REUSEADDR option on a socket. For example:

是的,它是有意的。在这里你可以阅读详细的解释。可以通过在套接字上设置 SO_REUSEADDR 选项来覆盖此行为。例如:

sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

回答by dirkk0

If you use a TCPServer, UDPServer or their subclasses in the SocketServer module, you can set this class variable (before instanciating a server):

如果在 SocketServer 模块中使用 TCPServer、UDPServer 或它们的子类,则可以设置此类变量(在实例化服务器之前):

SocketServer.TCPServer.allow_reuse_address = True

(via SocketServer.ThreadingTCPServer - Cannot bind to address after program restart)

(通过SocketServer.ThreadingTCPServer - 程序重启后无法绑定到地址

This causes the init(constructor) to:

这会导致init(构造函数):

 if self.allow_reuse_address:
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

回答by ayoub laaziz

$ ps -fA | grep python
501 81211 12368   0  10:11PM ttys000    0:03.12  
python -m SimpleHTTPServer

$ kill 81211

回答by Mirko

Nothing worked for me except running a subprocess with this command, before calling HTTPServer(('', 443), myHandler):

在调用之前,除了使用此命令运行子进程外,对我没有任何作用HTTPServer(('', 443), myHandler)

kill -9 $(lsof -ti tcp:443)

Of course this is only for linux-like OS!

当然,这仅适用于类似 linux 的操作系统!

回答by Siddharth Sethia

A simple solution that worked for me is to close the Terminal and restart it.

一个对我有用的简单解决方案是关闭终端并重新启动它。

回答by MohitGhodasara

because you trying to run service in same port that is already running.

因为您试图在已经运行的同一端口中运行服务。

some time its happen because your service is not stopped in process stack. you have to kill them

有一段时间它会发生,因为您的服务没有在进程堆栈中停止。你必须杀了他们

no need to install anythinghere is the one line command to kill all running python processes.

这里不需要安装任何东西,这是杀死所有正在运行的 python 进程的单行命令。

for Linux based OS:

对于基于 Linux 的操作系统:

Bash:

重击:

kill -9 $(ps -A | grep python | awk '{print }')

Fish:

鱼:

kill -9 (ps -A | grep python | awk '{print }')

回答by silvertech048

Got the same error :
Steps followed :
1 - used $ ps -fA | grep python
2 - Killed all the process
3 - Closed terminal
4 - relaunced and launched the application ( mkchromecast).
5 - did not get this error message.

Got another issue. following up on that .

得到相同的错误:
步骤如下:
1 - 使用$ ps -fA | grep python
2 -
终止所有进程3 - 关闭终端
4 - 重新启动并启动应用程序 (mkchromecast)。
5 - 未收到此错误消息。

有另一个问题。跟进那个。

回答by Manoj Kumar M

run the command

运行命令

fuser -k (port_number_you_are _trying_to_access)/TCP

example for flask: fuser -k 5000/tcp

烧瓶示例: fuser -k 5000/tcp

Also, remember this error arises when you interput by ctrl+z. so to terminate use ctrl+c

另外,请记住当您按 ctrl+z 输入时会出现此错误。所以要终止使用 ctrl+c

回答by Abdul Basit

First of all find the python process ID using this command

首先使用这个命令找到python进程ID

ps -fA | grep python

You will get a pid number by naming of your python process on second column

您将通过在第二列上命名您的 python 进程来获得一个 pid 编号

Then kill the process using this command

然后使用此命令终止进程

kill -9 pid