在编写一个socket程序时使用了如下代码
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
local_ip = socket.gethostbyname(socket.gethostname())
sock.bind((local_ip, mcast_group_port))
但在执行时却发生异常
Traceback (most recent call last):
File "r.py", line 42, in <module>
receiver()
File "r.py", line 18, in receiver
sock.bind((local_ip, mcast_group_port))
OSError: [Errno 99] Cannot assign requested address
意思就是说没办法分配获取到的IP地址,然后debug了一下获取到的IP地址以及主机查询到的IP地址果然存在冲突文章来源:https://www.toymoban.com/news/detail-517740.html
[root@panavm222 ~]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> local_ip = socket.gethostbyname(socket.gethostname())
>>> print(local_ip)
192.168.4.222
>>> exit()
[root@panavm222 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 56:6f:96:77:00:46 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.224/24 brd 192.168.4.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::d91e:d021:c49:87bb/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::c21a:ccf0:f5a3:3697/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::3841:10d6:b92f:ec7f/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
这个时候出现了有意思的地方,为什么代码获取的仍然是我修改ip之前的IP地址4.222,而不是修改之后的4.224,这个时候将这个已经不存在的4.222赋给绑定监听一定会报错的呢文章来源地址https://www.toymoban.com/news/detail-517740.html
到了这里,关于socket:OSError: [Errno 99] Cannot assign requested address的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!