Python [Errno 98] 地址已在使用中

发布于 2024-10-08 04:38:06 字数 154 浏览 0 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

情绪操控生活 2024-10-15 04:38:07

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

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

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

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

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

Of course this is only for linux-like OS!

誰ツ都不明白 2024-10-15 04:38:07

对于 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.

早茶月光 2024-10-15 04:38:07

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

ps -fA | grep python

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

,然后使用此命令终止该进程

kill -9 pid

First of all find the python process ID using this command

ps -fA | grep python

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

Then kill the process using this command

kill -9 pid
一袭水袖舞倾城 2024-10-15 04:38:07

运行 Flask 的命令

fuser -k (port_number_you_are _trying_to_access)/TCP

示例:fuser -k 5000/tcp

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

run the command

fuser -k (port_number_you_are _trying_to_access)/TCP

example for flask: fuser -k 5000/tcp

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

無心 2024-10-15 04:38:07

什么也不做,只需等待几分钟,问题就会得到解决。发生这种情况是由于某些进程缓慢终止,这就是为什么它甚至没有显示在正在运行的进程列表中。

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.

櫻之舞 2024-10-15 04:38:07

我在 odoo 服务器上遇到了类似的错误,并通过以下简单的步骤解决了该问题:

  1. 在终端中粘贴以下代码

    ps-fA | grep 蟒蛇

你会得到一个pid号。现在从终端输出的第二列复制 pid 号。

  1. 然后写如下

    杀死-9 pid

终端将重新启动,然后命令

flask run

将正常工作!
谢谢

I faced similar error at odoo server and resolved that with these simple following steps:

  1. 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.

  1. Then write as below

    kill -9 pid

The terminal will restart and then the command

flask run

Will work fine!
Thank you

分开我的手 2024-10-15 04:38:07

我尝试使用以下代码来解决该问题:

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

I tried the following code to settle the issue:

sudo lsof -t -i tcp:8000 | xargs kill -9
橘虞初梦 2024-10-15 04:38:07

我在运行 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.

私野 2024-10-15 04:38:07

使套接字立即可重用的最简洁方法是遵循建议,首先关闭连接的客户端(套接字),并确保服务器端最后关闭(如果需要的话通过异常处理)。

这很可能意味着服务器端永远运行。

如果“永远”循环暂停执行(例如从套接字读取),那么这不是问题。

只要没有客户端,如何“打破”“永远”循环取决于服务器管理员(除了明显的系统级例外)

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)

山色无中 2024-10-15 04:38:07

在终端

检查程序的PID:

$ netstat -tulpn

# output:

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
                   
#...
udp        0      0 127.0.0.1:20001         0.0.0.0:*                           2859/python 

在本例中端口:20001,因此PID为2859

终止此端口:

$ kill -9 2859

in Terminal

Check PID of program:

$ netstat -tulpn

# output:

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
                   
#...
udp        0      0 127.0.0.1:20001         0.0.0.0:*                           2859/python 

in this case port: 20001, so PID is 2859

terminate this port:

$ kill -9 2859
烟─花易冷 2024-10-15 04:38:07

sudo pkill -9 python

尝试这个命令

sudo pkill -9 python

try this command

难得心□动 2024-10-15 04:38:06

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

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

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:

sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
提笔书几行 2024-10-15 04:38:06
$ ps -fA | grep python
501 81211 12368   0  10:11PM ttys000    0:03.12  
python -m SimpleHTTPServer

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

$ kill 81211
橘虞初梦 2024-10-15 04:38:06

发生这种情况是因为您尝试在同一端口运行服务并且已经有一个正在运行的应用程序。
发生这种情况是因为您的服务未在进程堆栈中停止。你只需要杀死这些进程。

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

对于基于 Linux 的操作系统:

Bash:

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

Fish:

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

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:

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

Fish:

kill -9 (ps -A | grep python | awk '{print $1}')
飞烟轻若梦 2024-10-15 04:38:06

如果您在 socketserver 模块中使用 TCPServerUDPServer 或其子类,则可以设置此类变量(在实例化服务器之前)

socketserver.TCPServer.allow_reuse_address = True

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

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

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

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

socketserver.TCPServer.allow_reuse_address = True

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

This causes the init (constructor) to:

 if self.allow_reuse_address:
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
画▽骨i 2024-10-15 04:38:06

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文