‘OSError: [Errno 98] Address already in use’ 导致的是哪个Python异常?

huangapple go评论61阅读模式
英文:

What Python exception does 'OSError: [Errno 98] Address already in use' cause

问题

I want to catch the specific exception listed above, what do I need to put after except to do that?

我想捕获上面列出的特定异常,在except之后需要放什么?

英文:

I'm writing a basic Python TCP socket server. I want to catch the specific exception listed above, what do I need to put after except to do that?

答案1

得分: 1

"Its the OSError, so you need to catch OSError:"

except OSError as error:
    print(error)
英文:

Its the OSError, so you need to catch OSError:

except OSError as error:
    print(error)

答案2

得分: 1

错误的意思是有一个现有的进程正在使用与您在代码中使用的相同的IP地址和端口。

回答您最初的问题,这是如何在Python中处理此异常的方式:

try:
    # 在这里放入您的代码
except OSError as error:
    print(error)

然而,这不会解决您的问题,捕获错误也没有太多意义(除非您想要优雅地退出并显示一条消息)。您只需在任务管理器中找到使用相同地址的进程并将其终止。

英文:

The error means that there is an existing process using the same ip address and port that you're using within your code.

To answer your initial question, this is how you would handle this exception in Python:

try :
    #your code here 
except OSError as error :
    print(error)

Nevertheless, this will not solve your issue, and it doesn't make much sense to catch the error (only if you want to exit gracefully and show a message maybe). You just need to find the process which is using the same address in your task manager and kill it.

huangapple
  • 本文由 发表于 2023年5月6日 19:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定