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

时间:2018-01-17 17:11:55

标签: python sockets networking

我写了一个聊天服务器,但我无法将我的套接字绑定到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)
...

我收到以下错误:

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

我的代码出了什么问题?

我没有找到答案:

'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?

1 个答案:

答案 0 :(得分:5)

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

  

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

错误的IP地址导致了这种情况。你可以使用命令 ifconfig 检查您的计算机是否具有此IP地址。