Python [Errno 98] 地址已在使用中
在我的Python套接字程序中,我有时需要使用Ctrl-C来中断它。当我这样做时,它确实使用 socket.close()
关闭连接。
但是,当我尝试重新打开它时,我必须等待一分钟才能再次连接。如何正确关闭套接字?或者这是故意的?
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()
.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
除了在调用
HTTPServer(('', 443), myHandler)
之前使用此命令运行子进程之外,没有什么对我有用:当然,这仅适用于类似 linux 的操作系统!
Nothing worked for me except running a subprocess with this command, before calling
HTTPServer(('', 443), myHandler)
:Of course this is only for linux-like OS!
对于 Linux,
ps aux | grep python
这将显示错误。包含 python 文件的进程号(例如 35225)就是错误。
现在,
sudo Kill -9 35225
这将终止错误进程,您的问题将得到解决。
For Linux,
ps aux | grep python
This will show you the error. The process number (eg.35225) containing your python file is the error.
Now,
sudo kill -9 35225
This will kill the error process and your problem will be solved.
首先使用此命令找到 python 进程 ID
您将通过在第二列上命名 python 进程来获得 pid 号
,然后使用此命令终止该进程
First of all find the python process ID using this command
You will get a pid number by naming of your python process on second column
Then kill the process using this command
运行 Flask 的命令
示例:
fuser -k 5000/tcp
另外,请记住当您按 ctrl+z 输入时会出现此错误。所以要终止使用 ctrl+c
run the command
example for flask:
fuser -k 5000/tcp
Also, remember this error arises when you interput by ctrl+z. so to terminate use ctrl+c
什么也不做,只需等待几分钟,问题就会得到解决。发生这种情况是由于某些进程缓慢终止,这就是为什么它甚至没有显示在正在运行的进程列表中。
Do nothing just wait for a couple of minutes and it will get resolved. It happens due to the slow termination of some processes, and that's why it's not even showing in the running processes list.
我在 odoo 服务器上遇到了类似的错误,并通过以下简单的步骤解决了该问题:
在终端中粘贴以下代码
ps-fA | grep 蟒蛇
你会得到一个pid号。现在从终端输出的第二列复制 pid 号。
然后写如下
杀死-9 pid
终端将重新启动,然后命令
将正常工作!
谢谢
I faced similar error at odoo server and resolved that with these simple following steps:
Paste following code in terminal
ps -fA | grep python
You will get a pid number. Now copy the pid number from second column of terminal output.
Then write as below
kill -9 pid
The terminal will restart and then the command
Will work fine!
Thank you
我尝试使用以下代码来解决该问题:
I tried the following code to settle the issue:
我在运行 python 的 Raspberry Pi 上遇到了同样的问题(Err98 地址已在使用中),用于特斯拉墙壁连接器的电动汽车充电管理器。该软件以前一直很好,但有一天它停止询问太阳能逆变器,我花了几天时间认为这是我用 python 完成的事情。事实证明,根本原因是由于我家引入了新的智能电视,Wifi 调制解调器为太阳能逆变器分配了新的动态 IP。我更改了 python 代码以反映从 wifi 调制解调器和宾果游戏中找到的新 IP 地址,问题得到解决。
I had the same problem (Err98 Address already in use) on a Raspberry Pi running python for a EV charging manager for a Tesla Wall Connector. The software had previously been fine but it stopped interrogating the solar inverter one day and I spent days thinking it was something I'd done in python. Turns out the root cause was the Wifi modem assigning a new dynamic IP to the solar inverter as as result of introducing a new smart TV into my home. I changed the python code to reflect the new IP address that I found from the wifi modem and bingo, the issue was fixed.
使套接字立即可重用的最简洁方法是遵循建议,首先关闭连接的客户端(套接字),并确保服务器端最后关闭(如果需要的话通过异常处理)。
这很可能意味着服务器端永远运行。
如果“永远”循环暂停执行(例如从套接字读取),那么这不是问题。
只要没有客户端,如何“打破”“永远”循环取决于服务器管理员(除了明显的系统级例外)
The cleanest way to make the socket immediately reusable is to follow the recommendation to first shutdown the client end (socket) of a connection, and make sure the server's end shuts down last (through exception handling if needed).
This might well mean that the server end runs forever.
This is not a problem if that "forever" loop pauses execution, e.g. read from socket.
How you "break" that "forever" loop is up to you as server admin, as long as there are no clients (apart from obvious system level exceptions)
在终端
检查程序的PID:
在本例中端口:20001,因此PID为2859
终止此端口:
in Terminal
Check PID of program:
in this case port: 20001, so PID is 2859
terminate this port:
sudo pkill -9 python
尝试这个命令
sudo pkill -9 python
try this command
是的,这是有意的。您可以在这里阅读详细说明。可以通过在套接字上设置
SO_REUSEADDR
选项来覆盖此行为。例如:Yes, this is intended. Here you can read a detailed explanation. It is possible to override this behavior by setting the
SO_REUSEADDR
option on a socket. For example:发生这种情况是因为您尝试在同一端口运行服务并且已经有一个正在运行的应用程序。
发生这种情况是因为您的服务未在进程堆栈中停止。你只需要杀死这些进程。
无需安装任何东西这里是杀死所有正在运行的 python 进程的一行命令。
对于基于 Linux 的操作系统:
Bash:
Fish:
This happens because you trying to run service at the same port and there is an already running application.
it can happen because your service is not stopped in the process stack. you just have to kill those processes.
There is no need to install anything here is the one line command to kill all running python processes.
for Linux based OS:
Bash:
Fish:
如果您在
socketserver
模块中使用TCPServer
、UDPServer
或其子类,则可以设置此类变量(在实例化服务器之前):(通过 SocketServer.ThreadingTCPServer - 程序重新启动后无法绑定到地址 )
这会导致 init (构造函数):
If you use a
TCPServer
,UDPServer
or their subclasses in thesocketserver
module, you can set this class variable (before instantiating a server):(via SocketServer.ThreadingTCPServer - Cannot bind to address after program restart )
This causes the init (constructor) to:
对我有用的一个简单解决方案是关闭终端并重新启动它。
A simple solution that worked for me is to close the Terminal and restart it.