使用 python 在 Windows XP 中创建 IPv6 套接字
以下代码可在 Windows7 和 Linux 上运行,但在 Windows XP 中会出现错误:
import socket
print(socket.has_ipv6)
print("INET=",socket.AF_INET,"\nINET6=",socket.AF_INET6,"\nDGRAM=",socket.SOCK_DGRAM)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) #Error in XP
这是 Windows XP 中的输出:
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import socket
>>> print(socket.has_ipv6)
True
>>>
>>> print("INET=",socket.AF_INET,"\nINET6=",socket.AF_INET6,"\nDGRAM=",socket.SOCK_DGRAM)
INET= 2
INET6= 23
DGRAM= 2
>>>
>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s
<socket.socket object, fd=1840, family=2, type=2, proto=0>
>>>
>>>
>>> s6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\socket.py", line 94, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
socket.error: [Errno 10047] An address incompatible with the requested protocol was used
>>>
>>>
为什么我会收到此错误,如何修复此错误?
The following piece of code works on Windows7 and Linux, but gives an Error in Windows XP:
import socket
print(socket.has_ipv6)
print("INET=",socket.AF_INET,"\nINET6=",socket.AF_INET6,"\nDGRAM=",socket.SOCK_DGRAM)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) #Error in XP
This is the output in Windows XP:
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import socket
>>> print(socket.has_ipv6)
True
>>>
>>> print("INET=",socket.AF_INET,"\nINET6=",socket.AF_INET6,"\nDGRAM=",socket.SOCK_DGRAM)
INET= 2
INET6= 23
DGRAM= 2
>>>
>>> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> s
<socket.socket object, fd=1840, family=2, type=2, proto=0>
>>>
>>>
>>> s6 = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\socket.py", line 94, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
socket.error: [Errno 10047] An address incompatible with the requested protocol was used
>>>
>>>
Why do i get this Error, and how can i fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的计算机上需要安装 IPv6。 说明位于此处。(谢谢,David)
IPv6 needs to be installed on your machine. Instructions are here. (Thanks, David)