python error : 'module' object has no attribute 'AF_UNIX'

↘锁芯ラ 提交于 2021-02-07 07:26:46

问题


this is my python code :

if __name__ == '__main__':  
    import socket  
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)  
    sock.connect(('0.0.0.0', 4000))  
    import time  
    time.sleep(2)  
    #sock.send('1')
    print sock.recv(1024)  
    sock.close()  

it show :

Traceback (most recent call last):
  File "D:\Program Files\test\test\python\client.py", line 3, in <module>
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
AttributeError: 'module' object has no attribute 'AF_UNIX'

what can i do ,

thanks

updated:

Traceback (most recent call last):
  File "D:\Program Files\test\test\python\client.py", line 4, in <module>
    sock.connect(('0.0.0.0', 4000))
  File "<string>", line 1, in connect
socket.error: (10049, "Can't assign requested address")

回答1:


While creating a socket object on Windows you should do:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

AF_INET for Internet addresses, and AF_UNIX for UNIX inter-process communication. The latter is obviously only available on UNIX platforms.

Also, follow this example to find how to implement a simple socket server and client.



来源:https://stackoverflow.com/questions/5088087/python-error-module-object-has-no-attribute-af-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!