Python - socket.error:无法分配请求的地址

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

Python - socket.error: Cannot assign requested address

pythonsocketsnetworking

提问by llllllllll

I have written a chat server but I cannot bind my socket to an IP address:

我编写了一个聊天服务器,但我无法将套接字绑定到 IP 地址:

import sys
import os
import socket

HOST = "194.118.168.131"
SOCKET_LIST = []
RECV_BUFFER = 4096 
PORT = 9009

def chat_server():

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind((HOST, PORT))
    server_socket.listen(10)
...

I get the following error:

我收到以下错误:

Traceback (most recent call last):
  File "server.py", line 83, in <module>
    sys.exit(chat_server())
  File "server.py", line 20, in chat_server
    server_socket.bind((HOST, PORT))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address

What's wrong with my code?

我的代码有什么问题?



I didn't find an answer on:

我没有找到答案:

'Connection aborted.', error(99, 'Cannot assign requested address') error in Python,
socket.error[Errno 99] Cannot assign requested address,
bind: cannot assign requested address
Cannot assign requested address - possible causes?

'连接中止。',错误(99,'无法分配请求的地址')Python中的错误
socket.error [Errno 99]无法分配请求的地址
绑定:无法分配请求的地址
无法分配请求的地址 - 可能的原因?

回答by llllllllll

By checking errno.h, errno 99 is EADDRNOTAVAIL. The man page bind(2)says:

通过检查errno.h, errno 99 是EADDRNOTAVAIL。手册页bind(2)说:

EADDRNOTAVAIL A nonexistent interface was requested or the requested address was not local.

EADDRNOTAVAIL 请求了一个不存在的接口或请求的地址不是本地的。

It is often caused by a wrong IP address. You can use the command ifconfigto check whether your machine has this IP address.

它通常是由错误的 IP 地址引起的。您可以使用ifconfig命令 来检查您的机器是否有这个 IP 地址。